Skip to content

Commit 7cadd32

Browse files
committed
Fix choice keys and values for custom field types
Choices labels are defined as the keys of the array as of Symfony 3 (see symfony/symfony#14050).
1 parent dbee0be commit 7cadd32

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

form/create_custom_field_type.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
3333
{
3434
$resolver->setDefaults(array(
3535
'choices' => array(
36-
'm' => 'Male',
37-
'f' => 'Female',
36+
'Male' => 'm',
37+
'Female' => 'f',
3838
)
3939
));
4040
}
@@ -309,8 +309,13 @@ example, suppose that you're storing the gender parameters in configuration:
309309
.. code-block:: php
310310
311311
// app/config/config.php
312-
$container->setParameter('genders.m', 'Male');
313-
$container->setParameter('genders.f', 'Female');
312+
$container->setParameter(
313+
'genders',
314+
array(
315+
'm' => 'Male',
316+
'f' => 'Female',
317+
)
318+
);
314319
315320
To use the parameter, define your custom field type as a service, injecting
316321
the ``genders`` parameter value as the first argument to its to-be-created
@@ -384,7 +389,7 @@ configuration::
384389
public function configureOptions(OptionsResolver $resolver)
385390
{
386391
$resolver->setDefaults(array(
387-
'choices' => $this->genderChoices,
392+
'choices' => array_flip($this->genderChoices),
388393
));
389394
}
390395

0 commit comments

Comments
 (0)