Skip to content

[Form] Fix choice keys and values for custom field types #7940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
{
$resolver->setDefaults(array(
'choices' => array(
'm' => 'Male',
'f' => 'Female',
'Male' => 'm',
'Female' => 'f',
)
));
}
Expand Down Expand Up @@ -309,8 +309,13 @@ example, suppose that you're storing the gender parameters in configuration:
.. code-block:: php

// app/config/config.php
$container->setParameter('genders.m', 'Male');
$container->setParameter('genders.f', 'Female');
$container->setParameter(
'genders',
array(
'm' => 'Male',
'f' => 'Female',
)
);

To use the parameter, define your custom field type as a service, injecting
the ``genders`` parameter value as the first argument to its to-be-created
Expand Down Expand Up @@ -384,7 +389,7 @@ configuration::
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => $this->genderChoices,
'choices' => array_flip($this->genderChoices),
));
}

Expand Down