-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Symfony Forms ChoiceType chained choice_filter #60346
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
Comments
Hi Rafael, all form options are chainable by design (using the natural parent-child hierarchy and DI registration priority) For example you can add a global type extension for $resolver->setDefault('choice_filter', function (Options $options, mixed $previousFilter): string {
if (is_subclass_of($options['class'], DeprecationAwareInterface::class)) {
return fn (DeprecationAwareInterface $myEnum) => !$myEnum->isDeprecated();
}
// Take default value configured in the base class
return $previousValue;
}); Does this address your request? |
Hi Yonel, nice, I didn't know this! But this only sets a default option, so if a developer defines a choice_filter on the field itself our extension is overwritten. I would like to define a default choice_filter which is always applied first. If it returns true it should step into the field-level choice_filter, if it's defined. Probably it's somehow possible via DependencyInejction, maybe by decorating |
You could use $resolver->setNormalizer('choice_filter', function (Options $options, mixed $userFilter) {
return new CompositeChoiceFilter(...$this->defaultFilters, $userFilter);
}); Being |
Description
We have a lot of fields which use
EnumType
. Now some options start to become deprecated. We still need the enum cases to show them for previous form submissions but they shouldn't be select-able for future form submissions anymore. We now solve this by implementingon our Enum and then use
to filter the available options.
It would be a nice DX improvement if we could handle this on a more global level (ChoiceType TypeExtension). Maybe by making the choice_filter chainable. Then I could add a global filter with high priority to the chain checking for
instanceof DeprecationAwareInterface
.We're not using PHP 8.4 yet, but I would like to use the native
#[\Deprecated]
attribute as soon as we switch to 8.4 which would also nicely come into play for determining the deprecated options.Example
No response
The text was updated successfully, but these errors were encountered: