-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Refactored choice lists to support dynamic label, value, index and attribute generation #14050
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
Conversation
<3 |
@webmozart Do you plan to add |
#6456 is fixed here now. |
@webmozart an initial work for |
#8658 is fixed here now. |
// Due to a bug in OptionsResolver, the choices haven't been | ||
// validated yet at this point. Remove the if statement once that | ||
// bug is resolved | ||
if (!$options['choice_loader'] instanceof ChoiceLoaderInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be removed as I explained in the previous pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed
Fixed regression with #10572. |
*/ | ||
interface ChoiceListInterface | ||
interface ChoiceListInterface extends \Symfony\Component\Form\ChoiceList\ChoiceListInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a "use" statement and an alias instead of the FQCN here (like done everywhere else in Symfony)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must also add the deprecation notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed. I can't add a deprecation notice here (since it's an interface), but I'm adding them everywhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any call to trigger_error()
in this patch, so deprecation notices are not in place as far as I can tell. Am I missing something here?
89b30e3
to
91550c9
Compare
|
||
$choiceList = function (Options $options, $choiceList) use ($choiceListFactory) { | ||
if (null !== $choiceList) { | ||
trigger_error('The "choice_list" option is deprecated since version 2.7 and will be removed in 3.0. Use "choice_loader" instead.', E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think this will be triggered if people actually use the option as it will be overwritten. so I guess this needs to be done in a normalizer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
This PR was merged into the 2.7 branch. Discussion ---------- Issue4067 class triggers | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This is #14201 rebased on latest 2.7. This PR moves the deprecation notice triggers introduced in #14050 to file level, as [suggested](#14050 (comment)) by @stof. Commits ------- a3917fc [Form] Removed remaining deprecation notices in the test suite 8f297c1 [Form] Moved deprecation notice triggers to file level
the label of activity type is now handled by translatableStringHelper. Reasons are also grouped by categories. This is thanks to PR symfony/symfony#14050
@webmozart What is this ominous, yet intriguing
|
@likeitlikeit this is an example about how it could work to choose between value objects (assuming this is the way to build your value objects) |
@stof Is it a unicorn? It looks so beautiful... |
@likeitlikeit like https://gist.github.com/xabbuh/480c98a11b78285737ce, for example |
Is the caching behavior documented anywhere? |
Why are we being forced to do things backwards with the You could have extended ChoiceType to build out a ChoiceAsValueType (or ObjectType) with all the new options and left ChoiceType alone (like you should in OOP). Instead you bastardized a standard. Or you could have simply left |
Sorry for sounding so angry, but it's frustrating when a new feature is introduced that you're personally going to use 1% of the time, yet 100% of the time you'll have to think backwards. This could be built out like this: ChoiceType ObjectType EntityType (Optional) ObjectTypeInterface $builder->add('attending', 'choice', array(
'choices' => array(
Status::getInstance(Status::YES),
Status::getInstance(Status::NO),
Status::getInstance(Status::MAYBE),
),
'choice_label' => 'displayName',
'choice_name' => 'value'
)); you have $builder->add('attending', 'choice', array(
'choices' => array(
Status::getInstance(Status::YES),
Status::getInstance(Status::NO),
Status::getInstance(Status::MAYBE),
),
// internally, these are mapped to the following
// because "choices" is an array of ObjectTypeInterface
//'choice_label' => 'getChoiceLabel',
//'choice_name' => 'getChoiceName'
)); ChoiceType is still backwards compatible, and there's a new ObjectType that meshes well with EntityType. All the old and new functionality is there, and everyone is happy. |
This is a rebase of #12148 on the 2.7 branch.
I implemented the additional options "choice_label", "choice_name", "choice_value", "choice_attr", "group_by" and "choices_as_values" for ChoiceType. Additionally the "preferred_choices" option was updated to accept callables and property paths.
The "choices_as_values" option will be removed in Symfony 3.0, where the choices will be passed in the values of the "choices" option by default. The reason for that is that, right now, choices are limited to strings and integers (i.e. valid array keys). When we flip the array, we remove that limitation. Since choice labels are always strings, we can also always use them as array keys:
All the features described here obviously also apply to subtypes of "choice", such as "entity".
choice_label
Returns the label for each choice. Can be a callable (which receives the choice as first and the key of the "choices" array as second argument) or a property path.
If
null
, the keys of the "choices" array are used as labels.choice_name
Returns the form name for each choice. That name is used as name of the checkbox/radio form for this choice. It is also used as index of the choice views in the template. Can be a callable (like for "choice_label") or a property path.
The generated names must be valid form names, i.e. contain alpha-numeric symbols, underscores, hyphens and colons only. They must start with an alpha-numeric symbol or an underscore.
If
null
, an incrementing integer is used as name.choice_value
Returns the string value for each choice. This value is displayed in the "value" attributes and submitted in the POST/PUT requests. Can be a callable (like for "choice_label") or a property path.
If
null
, an incrementing integer is used as value.choice_attr
Returns the additional HTML attributes for choices. Can be an array, a callable (like for "choice_label") or a property path.
If an array, the key of the "choices" array must be used as keys.
group_by
Returns the grouping used for the choices. Can be an array/Traversable, a callable (like for "choice_label") or a property path.
The return values of the callable/property path are used as group labels. If
null
is returned, a choice is not grouped.If
null
, the structure of the "choices" array is used to construct the groups.preferred_choices
Returns the preferred choices. Can be an array/Traversable, a callable (like for "choice_label") or a property path.
Technical Changes
To properly implement all this, the old
ChoiceListInterface
class was deprecated and replaced by a new, slimmer one. The creation of choice views is now separated from choice lists. Hence a lot of logic is not executed anymore when processing (but not displaying) a form.Internally, a
ChoiceListFactoryInterface
implementation is used to construct choice lists and choice views. Two decorators exist for this class:CachingFactoryDecorator
: caches choice lists/views so that multiple fields displaying the same choices (e.g. in collection fields) use the same choice list/viewPropertyAccessDecorator
: adds support for property paths to a factoryBC Breaks
The option "choice_list" of ChoiceType now contains a
Symfony\Component\Form\ChoiceList\ChoiceListInterface
instance, which is a super-type of the deprecatedChoiceListInterface
.Todos