-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Replace passing type instances by class names #5321
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
The downside |
I don't like this downside either. |
@henrikbjorn Why your aversion against the DIC? Adding a type there is a two-liner... (three if you use XML). Additionally, you reduce complexity any time you use the type, since you don't have to take care of dependency handling anymore. If your type has dynamic dependencies that are not available in the DIC (such as the user), you can in fact use options. |
Btw, having FormType defined as service (but not tagged as form type) and injecting it into the controller to build a form from it, won't be supported anymore, too. Right? This approach does not suffer from any limitation you mention, e.g. no performance penalty since it's indeed a service. But it's wouldn't work anymore. Edit: I guess, using a service that is not tagged as form type, is still slower because the resolving to a ResolvedFormType could potentially be done multiple times? |
-1 deprecating this API ok, but removing it completly would be a a major punch in the face, as it will probably be quite some work to adjust applications. The problem is not having 1 form in the service container, it is a combination of context change (second file) and having N forms in my app. My apps normally have 1-2 times the number of forms that i have entities. |
@Tobion it does suffer from the same penalties as it is still re-adding the instance in the FormFactory each time. |
@beberlei I don't quite understand your -1? -1 on removing or -1 on deprecating? Of course, we would first deprecate the API in order to remove it later. |
Another thing we can do to ease the resulting pain is scanning all "Form" subnamespaces of all active bundles for form types. These types could then be accessed by name without declaring them as service. |
@bschussek but what if some of these types have dependencies ? It should probably check first if there is already a tagged service defined for them |
@stof If they have dependencies, they obviously cannot be auto-discovered. |
…sing of type instances (webmozart) This PR was merged into the 2.8 branch. Discussion ---------- [Form] Deprecated FormTypeInterface::getName() and passing of type instances | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #5321, #15008 | License | MIT | Doc PR | TODO #### Type Names This PR deprecates the definition of the `getName()` method of form types. See #15008 for a more detailed description. Before: ```php class MyType extends AbstractType { public function getName() { return 'mytype'; } // ... } ``` After: ```php class MyType extends AbstractType { // ... } ``` You should always reference other types by their fully-qualified class names. Thanks to PHP 5.5, that's easy: Before: ```php $form = $this->createFormBuilder() ->add('name', 'text') ->add('age', 'integer') ->getForm(); ``` After: ```php $form = $this->createFormBuilder() ->add('name', TextType::class) ->add('age', IntegerType::class) ->getForm(); ``` #### Type Instances Furthermore, passing of type instances is deprecated. Before: ```php $form = $this->createForm(new AuthorType()); ``` After: ```php $form = $this->createForm(AuthorType::class); ``` #### DIC Aliases When registering a type in the DIC, you should omit the "alias" attribute now. Before: ```xml <service id="my.type" class="Vendor\Type\MyType"> <tag name="form.type" alias="mytype" /> <argument type="service" id="some.service.id" /> </service> ``` After: ```xml <service id="my.type" class="Vendor\Type\MyType"> <tag name="form.type" /> <argument type="service" id="some.service.id" /> </service> ``` Types without dependencies don't need to be registered in the DIC as they can be instantiated right away. #### Template Block Prefixes By default, the class name of the type in underscore notation minus "Type" suffix is used as Twig template block prefix (e.g. `UserProfileType` => `user_profile_*`). If you want to customize that, overwrite the new `getBlockPrefix()` method in your type: ```php class UserProfileType extends AbstractType { public function getBlockPrefix() { return 'profile'; } // ... } ``` Commits ------- 3d9e5de [Form] Deprecated FormTypeInterface::getName() and passing of type instances
This ticket is a follow-up to #5221.
Currently, it is a common practice to use form types by instantiating them directly instead of registering them in the DIC:
This practice has a series of advantages:
But it also has limitations:
Because of these inconsistencies, I suggest to deprecate this API and replace it by the following:
The advantages of this approach are:
The downsides:
[1] assuming that https://wiki.php.net/rfc/class_name_scalars gets accepted
The text was updated successfully, but these errors were encountered: