[Form][choice] added choice_translation_domain to avoid trans options.

This commit is contained in:
Abdellatif Ait boudad 2015-04-01 12:46:44 +01:00
parent be0c98efaf
commit 5a33c2ca2e
14 changed files with 228 additions and 123 deletions

View File

@ -15,7 +15,7 @@ Router
`foo%bar%2` which would be compiled to `$foo % $bar % 2` in 2.6 `foo%bar%2` which would be compiled to `$foo % $bar % 2` in 2.6
but in 2.7 you would get an error if `bar` parameter but in 2.7 you would get an error if `bar` parameter
doesn't exist or unexpected result otherwise. doesn't exist or unexpected result otherwise.
Form Form
---- ----
@ -23,14 +23,14 @@ Form
AbstractType or AbstractExtensionType has been deprecated in favor of AbstractType or AbstractExtensionType has been deprecated in favor of
overriding the new "configureOptions" method. overriding the new "configureOptions" method.
The method "setDefaultOptions(OptionsResolverInterface $resolver)" will The method "setDefaultOptions(OptionsResolverInterface $resolver)" will
be renamed in Symfony 3.0 to "configureOptions(OptionsResolver $resolver)". be renamed in Symfony 3.0 to "configureOptions(OptionsResolver $resolver)".
Before: Before:
```php ```php
use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TaskType extends AbstractType class TaskType extends AbstractType
{ {
// ... // ...
@ -47,7 +47,7 @@ Form
```php ```php
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
class TaskType extends AbstractType class TaskType extends AbstractType
{ {
// ... // ...
@ -59,12 +59,12 @@ Form
} }
} }
``` ```
* The "choice_list" option of ChoiceType was deprecated. You should use * The "choice_list" option of ChoiceType was deprecated. You should use
"choices_as_values" or "choice_loader" now. "choices_as_values" or "choice_loader" now.
Before: Before:
```php ```php
$form->add('status', 'choice', array( $form->add('status', 'choice', array(
'choice_list' => new ObjectChoiceList(array( 'choice_list' => new ObjectChoiceList(array(
@ -74,9 +74,9 @@ Form
)), )),
)); ));
``` ```
After: After:
```php ```php
$form->add('status', 'choice', array( $form->add('status', 'choice', array(
'choices' => array( 'choices' => array(
@ -87,13 +87,13 @@ Form
'choices_as_values' => true, 'choices_as_values' => true,
)); ));
``` ```
* You should flip the keys and values of the "choices" option in ChoiceType * You should flip the keys and values of the "choices" option in ChoiceType
and set the "choices_as_values" option to `true`. The default value of that and set the "choices_as_values" option to `true`. The default value of that
option will be switched to `true` in Symfony 3.0. option will be switched to `true` in Symfony 3.0.
Before: Before:
```php ```php
$form->add('status', 'choice', array( $form->add('status', 'choice', array(
'choices' => array( 'choices' => array(
@ -103,9 +103,9 @@ Form
)), )),
)); ));
``` ```
After: After:
```php ```php
$form->add('status', 'choice', array( $form->add('status', 'choice', array(
'choices' => array( 'choices' => array(
@ -116,64 +116,64 @@ Form
'choices_as_values' => true, 'choices_as_values' => true,
)); ));
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface` was * `Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\ChoiceListInterface` instead. `Symfony\Component\Form\ChoiceList\ChoiceListInterface` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
public function doSomething(ChoiceListInterface $choiceList) public function doSomething(ChoiceListInterface $choiceList)
{ {
// ... // ...
} }
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
public function doSomething(ChoiceListInterface $choiceList) public function doSomething(ChoiceListInterface $choiceList)
{ {
// ... // ...
} }
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView` was * `Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\View\ChoiceView` instead. `Symfony\Component\Form\ChoiceList\View\ChoiceView` instead.
Note that the order of the arguments passed to the constructor was inverted. Note that the order of the arguments passed to the constructor was inverted.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView; use Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView;
$view = new ChoiceView($data, 'value', 'Label'); $view = new ChoiceView($data, 'value', 'Label');
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\View\ChoiceView; use Symfony\Component\Form\ChoiceList\View\ChoiceView;
$view = new ChoiceView('Label', 'value', $data); $view = new ChoiceView('Label', 'value', $data);
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList` was * `Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead. `Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
$choiceList = new ChoiceList( $choiceList = new ChoiceList(
array(Status::ENABLED, Status::DISABLED, Status::IGNORED), array(Status::ENABLED, Status::DISABLED, Status::IGNORED),
array('Enabled', 'Disabled', 'Ignored'), array('Enabled', 'Disabled', 'Ignored'),
@ -181,19 +181,19 @@ Form
array(Status::ENABLED), array(Status::ENABLED),
); );
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
$factory = new DefaultChoiceListFactory(); $factory = new DefaultChoiceListFactory();
$choices = array(Status::ENABLED, Status::DISABLED, Status::IGNORED); $choices = array(Status::ENABLED, Status::DISABLED, Status::IGNORED);
$labels = array('Enabled', 'Disabled', 'Ignored'); $labels = array('Enabled', 'Disabled', 'Ignored');
$choiceList = $factory->createListFromChoices($choices); $choiceList = $factory->createListFromChoices($choices);
$choiceListView = $factory->createView( $choiceListView = $factory->createView(
$choiceList, $choiceList,
// Preferred choices // Preferred choices
@ -204,75 +204,75 @@ Form
} }
); );
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList` was * `Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory::createListFromLoader()` `Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory::createListFromLoader()`
together with an implementation of together with an implementation of
`Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface` instead. `Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
class MyLazyChoiceList extends LazyChoiceList class MyLazyChoiceList extends LazyChoiceList
{ {
public function loadChoiceList() public function loadChoiceList()
{ {
// load $choiceList // load $choiceList
return $choiceList; return $choiceList;
} }
} }
$choiceList = new MyLazyChoiceList(); $choiceList = new MyLazyChoiceList();
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class MyChoiceLoader implements ChoiceLoaderInterface class MyChoiceLoader implements ChoiceLoaderInterface
{ {
// ... // ...
} }
$factory = new DefaultChoiceListFactory(); $factory = new DefaultChoiceListFactory();
$choiceList = $factory->createListFromLoader(new MyChoiceLoader()); $choiceList = $factory->createListFromLoader(new MyChoiceLoader());
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList` was * `Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead. `Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
$choiceList = new ObjectChoiceList( $choiceList = new ObjectChoiceList(
array(Status::getInstance(Status::ENABLED), Status::getInstance(Status::DISABLED)), array(Status::getInstance(Status::ENABLED), Status::getInstance(Status::DISABLED)),
// Label property // Label property
'name' 'name'
); );
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
$factory = new DefaultChoiceListFactory(); $factory = new DefaultChoiceListFactory();
$choiceList = $factory->createListFromChoices(array( $choiceList = $factory->createListFromChoices(array(
Status::getInstance(Status::ENABLED), Status::getInstance(Status::ENABLED),
Status::getInstance(Status::DISABLED), Status::getInstance(Status::DISABLED),
)); ));
$choiceListView = $factory->createView( $choiceListView = $factory->createView(
$choiceList, $choiceList,
// Preferred choices // Preferred choices
@ -281,34 +281,34 @@ Form
'name' 'name'
); );
``` ```
* `Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList` was * `Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead. `Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
$choiceList = new SimpleChoiceList(array( $choiceList = new SimpleChoiceList(array(
Status::ENABLED => 'Enabled', Status::ENABLED => 'Enabled',
Status::DISABLED => 'Disabled', Status::DISABLED => 'Disabled',
)); ));
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
$factory = new DefaultChoiceListFactory(); $factory = new DefaultChoiceListFactory();
$choices = array(Status::ENABLED, Status::DISABLED); $choices = array(Status::ENABLED, Status::DISABLED);
$labels = array('Enabled', 'Disabled'); $labels = array('Enabled', 'Disabled');
$choiceList = $factory->createListFromChoices($choices); $choiceList = $factory->createListFromChoices($choices);
$choiceListView = $factory->createView( $choiceListView = $factory->createView(
$choiceList, $choiceList,
// Preferred choices // Preferred choices
@ -319,116 +319,152 @@ Form
} }
); );
``` ```
* The "property" option of `DoctrineType` was deprecated. You should use the * The "property" option of `DoctrineType` was deprecated. You should use the
new inherited option "choice_label" instead, which has the same effect. new inherited option "choice_label" instead, which has the same effect.
Before: Before:
```php ```php
$form->add('tags', 'entity', array( $form->add('tags', 'entity', array(
'class' => 'Acme\Entity\MyTag', 'class' => 'Acme\Entity\MyTag',
'property' => 'name', 'property' => 'name',
)) ))
``` ```
After: After:
```php ```php
$form->add('tags', 'entity', array( $form->add('tags', 'entity', array(
'class' => 'Acme\Entity\MyTag', 'class' => 'Acme\Entity\MyTag',
'choice_label' => 'name', 'choice_label' => 'name',
)) ))
``` ```
* The "loader" option of `DoctrineType` was deprecated and will be removed in * The "loader" option of `DoctrineType` was deprecated and will be removed in
Symfony 3.0. You should override the `getLoader()` method instead in a custom Symfony 3.0. You should override the `getLoader()` method instead in a custom
type. type.
Before: Before:
```php ```php
$form->add('tags', 'entity', array( $form->add('tags', 'entity', array(
'class' => 'Acme\Entity\MyTag', 'class' => 'Acme\Entity\MyTag',
'loader' => new MyEntityLoader(), 'loader' => new MyEntityLoader(),
)) ))
``` ```
After: After:
class MyEntityType extends DoctrineType class MyEntityType extends DoctrineType
{ {
// ... // ...
public function getLoader() public function getLoader()
{ {
return new MyEntityLoader(); return new MyEntityLoader();
} }
} }
* `Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList` was * `Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader` instead. `Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader` instead.
Before: Before:
```php ```php
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
$choiceList = new EntityChoiceList($em, 'Acme\Entity\MyEntity'); $choiceList = new EntityChoiceList($em, 'Acme\Entity\MyEntity');
``` ```
After: After:
```php ```php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
$factory = new DefaultChoiceListFactory(); $factory = new DefaultChoiceListFactory();
$choices = array(Status::ENABLED, Status::DISABLED); $choices = array(Status::ENABLED, Status::DISABLED);
$labels = array('Enabled', 'Disabled'); $labels = array('Enabled', 'Disabled');
$choiceLoader = new DoctrineChoiceLoader($factory, $em, 'Acme\Entity\MyEntity'); $choiceLoader = new DoctrineChoiceLoader($factory, $em, 'Acme\Entity\MyEntity');
$choiceList = $factory->createListFromLoader($choiceLoader); $choiceList = $factory->createListFromLoader($choiceLoader);
``` ```
* Passing a query builder closure to `ORMQueryBuilderLoader` was deprecated and * Passing a query builder closure to `ORMQueryBuilderLoader` was deprecated and
will not be supported anymore in Symfony 3.0. You should pass resolved query will not be supported anymore in Symfony 3.0. You should pass resolved query
builders only. builders only.
Consequently, the arguments `$manager` and `$class` of `ORMQueryBuilderLoader` Consequently, the arguments `$manager` and `$class` of `ORMQueryBuilderLoader`
have been deprecated as well. have been deprecated as well.
Note that the "query_builder" option of `DoctrineType` *does* support Note that the "query_builder" option of `DoctrineType` *does* support
closures, but the closure is now resolved in the type instead of in the closures, but the closure is now resolved in the type instead of in the
loader. loader.
Before: Before:
``` ```
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
$queryBuilder = function () { $queryBuilder = function () {
// return QueryBuilder // return QueryBuilder
}; };
$loader = new ORMQueryBuilderLoader($queryBuilder); $loader = new ORMQueryBuilderLoader($queryBuilder);
``` ```
After: After:
``` ```
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
// create $queryBuilder // create $queryBuilder
$loader = new ORMQueryBuilderLoader($queryBuilder); $loader = new ORMQueryBuilderLoader($queryBuilder);
``` ```
* The classes `ChoiceToBooleanArrayTransformer`, * The classes `ChoiceToBooleanArrayTransformer`,
`ChoicesToBooleanArrayTransformer`, `FixRadioInputListener` and `ChoicesToBooleanArrayTransformer`, `FixRadioInputListener` and
`FixCheckboxInputListener` were deprecated and will be removed in Symfony 3.0. `FixCheckboxInputListener` were deprecated and will be removed in Symfony 3.0.
Their functionality is covered by the new classes `RadioListMapper` and Their functionality is covered by the new classes `RadioListMapper` and
`CheckboxListMapper`. `CheckboxListMapper`.
* The ability to translate Doctrine type entries by the translator component
is now disabled by default and to enable it you must explicitly set the option
"choice_translation_domain" to true
Before:
```
$form->add('products', 'entity', array(
'class' => 'AppBundle/Entity/Product',
));
```
After:
```
$form->add('products', 'entity', array(
'class' => 'AppBundle/Entity/Product',
'choice_translation_domain' => true,
));
```
* In the block `choice_widget_options` the `translation_domain` has been replaced
with the `choice_translation_domain` option.
Before:
```jinja
{{ choice.label|trans({}, translation_domain) }}
```
After:
```jinja
{{ choice_translation_domain is sameas(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}
```
Serializer Serializer
---------- ----------

View File

@ -292,6 +292,7 @@ abstract class DoctrineType extends AbstractType
'choice_name' => $choiceName, 'choice_name' => $choiceName,
'choice_value' => $choiceValue, 'choice_value' => $choiceValue,
'id_reader' => null, // internal 'id_reader' => null, // internal
'choice_translation_domain' => false,
)); ));
$resolver->setRequired(array('class')); $resolver->setRequired(array('class'));

View File

@ -74,13 +74,13 @@
{%- block choice_widget_options -%} {%- block choice_widget_options -%}
{% for group_label, choice in options %} {% for group_label, choice in options %}
{%- if choice is iterable -%} {%- if choice is iterable -%}
<optgroup label="{{ group_label|trans({}, translation_domain) }}"> <optgroup label="{{ choice_translation_domain is sameas(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %} {% set options = choice %}
{{- block('choice_widget_options') -}} {{- block('choice_widget_options') -}}
</optgroup> </optgroup>
{%- else -%} {%- else -%}
{% set attr = choice.attr %} {% set attr = choice.attr %}
<option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option> <option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is sameas(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
{%- endif -%} {%- endif -%}
{% endfor %} {% endfor %}
{%- endblock choice_widget_options -%} {%- endblock choice_widget_options -%}

View File

@ -2,12 +2,12 @@
$translatorHelper = $view['translator']; // outside of the loop for performance reasons! ?> $translatorHelper = $view['translator']; // outside of the loop for performance reasons! ?>
<?php $formHelper = $view['form']; ?> <?php $formHelper = $view['form']; ?>
<?php foreach ($choices as $index => $choice): ?> <?php foreach ($choices as $group_label => $choice): ?>
<?php if (is_array($choice) || $choice instanceof ChoiceGroupView): ?> <?php if (is_array($choice) || $choice instanceof ChoiceGroupView): ?>
<optgroup label="<?php echo $view->escape($translatorHelper->trans($index, array(), $translation_domain)) ?>"> <optgroup label="<?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($group_label, array(), $choice_translation_domain) : $group_label) ?>">
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?> <?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
</optgroup> </optgroup>
<?php else: ?> <?php else: ?>
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option> <option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($choice->label, array(), $choice_translation_domain) : $choice->label) ?></option>
<?php endif ?> <?php endif ?>
<?php endforeach ?> <?php endforeach ?>

View File

@ -4,6 +4,7 @@ CHANGELOG
2.7.0 2.7.0
----- -----
* added option "choice_translation_domain" to ChoiceType.
* deprecated option "precision" in favor of "scale" * deprecated option "precision" in favor of "scale"
* deprecated the overwriting of AbstractType::setDefaultOptions() in favor of overwriting AbstractType::configureOptions(). * deprecated the overwriting of AbstractType::setDefaultOptions() in favor of overwriting AbstractType::configureOptions().
* deprecated the overwriting of AbstractTypeExtension::setDefaultOptions() in favor of overwriting AbstractTypeExtension::configureOptions(). * deprecated the overwriting of AbstractTypeExtension::setDefaultOptions() in favor of overwriting AbstractTypeExtension::configureOptions().
@ -16,7 +17,7 @@ CHANGELOG
* deprecated ChoiceToBooleanArrayTransformer and ChoicesToBooleanArrayTransformer * deprecated ChoiceToBooleanArrayTransformer and ChoicesToBooleanArrayTransformer
* deprecated FixCheckboxInputListener and FixRadioInputListener * deprecated FixCheckboxInputListener and FixRadioInputListener
* deprecated the "choice_list" option of ChoiceType * deprecated the "choice_list" option of ChoiceType
* added new options to ChoiceType: * added new options to ChoiceType:
* "choices_as_values" * "choices_as_values"
* "choice_loader" * "choice_loader"
* "choice_label" * "choice_label"

View File

@ -158,6 +158,11 @@ class ChoiceType extends AbstractType
*/ */
public function buildView(FormView $view, FormInterface $form, array $options) public function buildView(FormView $view, FormInterface $form, array $options)
{ {
$choiceTranslationDomain = $options['choice_translation_domain'];
if ($view->parent && null === $choiceTranslationDomain) {
$choiceTranslationDomain = $view->vars['translation_domain'];
}
/** @var ChoiceListView $choiceListView */ /** @var ChoiceListView $choiceListView */
$choiceListView = $form->getConfig()->hasAttribute('choice_list_view') $choiceListView = $form->getConfig()->hasAttribute('choice_list_view')
? $form->getConfig()->getAttribute('choice_list_view') ? $form->getConfig()->getAttribute('choice_list_view')
@ -170,6 +175,7 @@ class ChoiceType extends AbstractType
'choices' => $choiceListView->choices, 'choices' => $choiceListView->choices,
'separator' => '-------------------', 'separator' => '-------------------',
'placeholder' => null, 'placeholder' => null,
'choice_translation_domain' => $choiceTranslationDomain,
)); ));
// The decision, whether a choice is selected, is potentially done // The decision, whether a choice is selected, is potentially done
@ -295,6 +301,14 @@ class ChoiceType extends AbstractType
return $options['expanded']; return $options['expanded'];
}; };
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
if (true === $choiceTranslationDomain) {
return $options['translation_domain'];
}
return $choiceTranslationDomain;
};
$resolver->setDefaults(array( $resolver->setDefaults(array(
'multiple' => false, 'multiple' => false,
'expanded' => false, 'expanded' => false,
@ -317,14 +331,17 @@ class ChoiceType extends AbstractType
// is manually set to an object. // is manually set to an object.
// See https://github.com/symfony/symfony/pull/5582 // See https://github.com/symfony/symfony/pull/5582
'data_class' => null, 'data_class' => null,
'choice_translation_domain' => true,
)); ));
$resolver->setNormalizer('choice_list', $choiceListNormalizer); $resolver->setNormalizer('choice_list', $choiceListNormalizer);
$resolver->setNormalizer('empty_value', $placeholderNormalizer); $resolver->setNormalizer('empty_value', $placeholderNormalizer);
$resolver->setNormalizer('placeholder', $placeholderNormalizer); $resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface')); $resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface'));
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable')); $resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
$resolver->setAllowedTypes('choices_as_values', 'bool'); $resolver->setAllowedTypes('choices_as_values', 'bool');
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')); $resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); $resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));

View File

@ -24,6 +24,7 @@ class CountryType extends AbstractType
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'choices' => Intl::getRegionBundle()->getCountryNames(), 'choices' => Intl::getRegionBundle()->getCountryNames(),
'choice_translation_domain' => false,
)); ));
} }

View File

@ -24,6 +24,7 @@ class CurrencyType extends AbstractType
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'choices' => Intl::getCurrencyBundle()->getCurrencyNames(), 'choices' => Intl::getCurrencyBundle()->getCurrencyNames(),
'choice_translation_domain' => false,
)); ));
} }

View File

@ -24,6 +24,7 @@ class LanguageType extends AbstractType
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'choices' => Intl::getLanguageBundle()->getLanguageNames(), 'choices' => Intl::getLanguageBundle()->getLanguageNames(),
'choice_translation_domain' => false,
)); ));
} }

View File

@ -24,6 +24,7 @@ class LocaleType extends AbstractType
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'choices' => Intl::getLocaleBundle()->getLocaleNames(), 'choices' => Intl::getLocaleBundle()->getLocaleNames(),
'choice_translation_domain' => false,
)); ));
} }

View File

@ -30,6 +30,7 @@ class TimezoneType extends AbstractType
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'choices' => self::getTimezones(), 'choices' => self::getTimezones(),
'choice_translation_domain' => false,
)); ));
} }

View File

@ -840,7 +840,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
'/select '/select
[@name="name"] [@name="name"]
[@class="my&class form-control"] [@class="my&class form-control"]
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]] [./option[@value="AT"][@selected="selected"][.="Austria"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -858,7 +858,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
[@name="name"] [@name="name"]
[@class="my&class form-control"] [@class="my&class form-control"]
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]] [./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]]
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]] [./option[@value="AT"][@selected="selected"][.="Austria"]]
[count(./option)>201] [count(./option)>201]
' '
); );
@ -1388,7 +1388,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
'/select '/select
[@name="name"] [@name="name"]
[@class="my&class form-control"] [@class="my&class form-control"]
[./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]] [./option[@value="de"][@selected="selected"][.="German"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -1402,7 +1402,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
'/select '/select
[@name="name"] [@name="name"]
[@class="my&class form-control"] [@class="my&class form-control"]
[./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]] [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -1826,8 +1826,8 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
[@class="my&class form-control"] [@class="my&class form-control"]
[not(@required)] [not(@required)]
[./optgroup [./optgroup
[@label="[trans]Europe[/trans]"] [@label="Europe"]
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]] [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
] ]
[count(./optgroup)>10] [count(./optgroup)>10]
[count(.//option)>200] [count(.//option)>200]

View File

@ -1016,7 +1016,7 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
'/select '/select
[@name="name"] [@name="name"]
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]] [./option[@value="AT"][@selected="selected"][.="Austria"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -1033,7 +1033,7 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
'/select '/select
[@name="name"] [@name="name"]
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]] [./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]]
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]] [./option[@value="AT"][@selected="selected"][.="Austria"]]
[count(./option)>201] [count(./option)>201]
' '
); );
@ -1557,7 +1557,7 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
'/select '/select
[@name="name"] [@name="name"]
[./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]] [./option[@value="de"][@selected="selected"][.="German"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -1570,7 +1570,7 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
'/select '/select
[@name="name"] [@name="name"]
[./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]] [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
[count(./option)>200] [count(./option)>200]
' '
); );
@ -1936,8 +1936,8 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
[@name="name"] [@name="name"]
[not(@required)] [not(@required)]
[./optgroup [./optgroup
[@label="[trans]Europe[/trans]"] [@label="Europe"]
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]] [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
] ]
[count(./optgroup)>10] [count(./optgroup)>10]
[count(.//option)>200] [count(.//option)>200]

View File

@ -1331,6 +1331,51 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertTrue($view->vars['expanded']); $this->assertTrue($view->vars['expanded']);
} }
public function testPassChoiceTranslationDomainToView()
{
$form = $this->factory->create('choice', null, array(
'choices' => $this->choices,
));
$view = $form->createView();
$this->assertNull($view->vars['choice_translation_domain']);
}
public function testChoiceTranslationDomainWithTrueValueToView()
{
$form = $this->factory->create('choice', null, array(
'choices' => $this->choices,
'choice_translation_domain' => true,
));
$view = $form->createView();
$this->assertNull($view->vars['choice_translation_domain']);
}
public function testDefaulChoiceTranslationDomainIsSameAsTranslationDomainToView()
{
$form = $this->factory->create('choice', null, array(
'choices' => $this->choices,
'translation_domain' => 'foo',
));
$view = $form->createView();
$this->assertEquals('foo', $view->vars['choice_translation_domain']);
}
public function testInheritChoiceTranslationDomainFromParent()
{
$view = $this->factory
->createNamedBuilder('parent', 'form', null, array(
'translation_domain' => 'domain',
))
->add('child', 'choice')
->getForm()
->createView();
$this->assertEquals('domain', $view['child']->vars['choice_translation_domain']);
}
public function testPlaceholderIsNullByDefaultIfRequired() public function testPlaceholderIsNullByDefaultIfRequired()
{ {
$form = $this->factory->create('choice', null, array( $form = $this->factory->create('choice', null, array(