8000 [Form] Ability to extend multiple form types with one type extension · Issue #27906 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Ability to extend multiple form types with one type extension #27906

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
xabbuh opened this issue Jul 9, 2018 · 8 comments
Closed

[Form] Ability to extend multiple form types with one type extension #27906

xabbuh opened this issue Jul 9, 2018 · 8 comments
Labels
Form RFC RFC = Request For Comments (proposals about features that you want to be discussed)

Comments

@xabbuh
Copy link
Member
xabbuh commented Jul 9, 2018

Currently, it is only possible to extend a single form type (and all its children) with a single form type extension. If you want to add an additional option to, for example, TextType and ChoiceType you will either have to create two extension classes or create an extension for the base FormType and thus adding the option to most other form types too.

@HeahDude and me discovered that we need to discuss if we want to support these uses cases in Form component before we can make a final decision on the implementation in #24530. Otherwise, we risk to deprecate the getExtendedType() method of the FormTypeExtensionInterface in favour of a solution which would then in turn be deprecated again in one of the next minor releases just to support the extension of multiple form types. That wouldn't be good DX-wise.

@xabbuh xabbuh added Form RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Jul 9, 2018
@yceruto
Copy link
Member
yceruto commented Jul 9, 2018

I personally have never needed it, but I guess there could be a good reason to do it.

Btw, there is another alternative to achieve it #9507 (comment):

So, since there is a solution that works right now (abstract type + subclasses) - even thought it's not the nicest one - I don't think this issue is critical.

@HeahDude
Copy link
Contributor
8000

The issue is not critical yes, means it is not worth working on a bc layer for itself.

IMHO the use case is legit, and modifying the interface already to handle auto configuration is a good opportunity to support it.

Abstract class and trait can be used currently yes, but I never saw that in action either. However I did see some $builder->getType()->getInnerType() or $form->getConfig()->getType()->getInnerType() in extensions. Even if that works, doing so adds a lot of overhead on unrelated forms/fields.

Anyway, having a clean and self explicit way to do it will prevent some trouble figuring out how this can be achieved and how ugly this can get.

@javiereguiluz
Copy link
Member

In order to be able to evaluate this proposal better, could you please share with us some real-world examples where extending multiple form types at the same time would be needed? Thanks!

@HeahDude
Copy link
Contributor
HeahDude commented Jul 10, 2018

I can imagine a real example without having the need to do something like that yet.

We usually make a text type extension in our application to add some options that will trigger some normalizers on submission. That's cool because all types that are a child of TextType like EmailType, TextareaType, etc. have these options too. Even third party ones as long as TextType is their parent.

Here is the scenario. Now we install a new dependency that comes with a type which does not extend TextType, but we would like to use that extension we have.

Currently we need to:

  • either move the logic in a trait or an abstract class, and create another extension to extend the new type
  • or extend FormType instead of TextType and perform type checking (brings complexity and needless overhead)

With such new feature it's just one line to declare another type class in the static method.

@javiereguiluz
Copy link
Member

@HeahDude I'm afraid your example is still a theoretical scenario. We need real-world examples where this feature would have been nice to have. I don't use the Form component, but I know that you and others use it a lot ... so, have you ever wished this feature existed to solve a real problem? Thanks!

@vudaltsov
Copy link
Contributor

When I made #25582, I introduced a new input=datetime_immutable option for 3 date/time types.
I also made a polyfill for this (https://github.com/ruvents/ruwork-polyfill-form-dti) where I had to write 1 abstract + 3 concrete extensions instead of 1 concrete. Looks like a real example!

@vudaltsov
Copy link
Contributor

More than that now with Symfony 4.1 I am always using DateTimeImmutable in entities and to have input=datetime_immutable by default I also have a 1+3 workaround with $resolver->setDefault('input', 'datetime_immutable').

@vudaltsov
Copy link
Contributor

Sometimes both FormType and ButtonType might be extended. In core we have Symfony\Component\Form\Extension\Validator\Type\BaseValidatorExtension -> FormTypeValidatorExtension, SubmitTypeValidatorExtension which almost represents that situation (but not an ideal example because it's changed in one of subclasses).

@fabpot fabpot closed this as completed Oct 10, 2018
fabpot added a commit that referenced this issue Oct 10, 2018
…xabbuh)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Form] simplify the form type extension registration

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22833, #27906
| License       | MIT
| Doc PR        |

Commits
-------

6a1d4c5 simplify the form type extension registration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Form RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

No branches or pull requests

6 participants
0