-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add default implementation for AbstractType::getName() #15008
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
I think this is a nice idea for services, but what about multiple services that automatically get the same alias? |
form names don't need to be unique (unless you try to submit them in the same request, which would be really weird). And type names will not conflict by default as the default value would be the FQCN |
@stof so the Alias would default to |
Implemented in #15079. |
…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
As originally discussed in #11185, a default implementation should be added for
AbstractType::getName()
:getName()
should return the type's FQCN by defaultBefore:
After:
The text was updated successfully, but these errors were encountered: