Skip to content

Commit df0c069

Browse files
committed
feature #15079 [Form] Deprecated FormTypeInterface::getName() and passing 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
2 parents 6acc5bb + 60065a3 commit df0c069

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

Tests/Functional/Bundle/CsrfFormLoginBundle/Controller/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LoginController extends ContainerAware
1919
{
2020
public function loginAction()
2121
{
22-
$form = $this->container->get('form.factory')->create('user_login');
22+
$form = $this->container->get('form.factory')->create('Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form\UserLoginType');
2323

2424
return $this->container->get('templating')->renderResponse('CsrfFormLoginBundle:Login:login.html.twig', array(
2525
'form' => $form->createView(),

Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginFormType.php renamed to Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginType.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @author Henrik Bjornskov <[email protected]>
2828
* @author Jeremy Mikola <[email protected]>
2929
*/
30-
class UserLoginFormType extends AbstractType
30+
class UserLoginType extends AbstractType
3131
{
3232
private $requestStack;
3333

@@ -45,9 +45,9 @@ public function __construct(RequestStack $requestStack)
4545
public function buildForm(FormBuilderInterface $builder, array $options)
4646
{
4747
$builder
48-
->add('username', 'text')
49-
->add('password', 'password')
50-
->add('_target_path', 'hidden')
48+
->add('username', 'Symfony\Component\Form\Extension\Core\Type\TextType')
49+
->add('password', 'Symfony\Component\Form\Extension\Core\Type\PasswordType')
50+
->add('_target_path', 'Symfony\Component\Form\Extension\Core\Type\HiddenType')
5151
;
5252

5353
$request = $this->requestStack->getCurrentRequest();
@@ -87,12 +87,4 @@ public function configureOptions(OptionsResolver $resolver)
8787
'intention' => 'authenticate',
8888
));
8989
}
90-
91-
/**
92-
* {@inheritdoc}
93-
*/
94-
public function getName()
95-
{
96-
return 'user_login';
97-
}
9890
}

Tests/Functional/app/CsrfFormLogin/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ imports:
33

44
services:
55
csrf_form_login.form.type:
6-
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form\UserLoginFormType
6+
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form\UserLoginType
77
arguments:
88
- @request_stack
99
tags:
10-
- { name: form.type, alias: user_login }
10+
- { name: form.type }
1111

1212
security:
1313
encoders:

0 commit comments

Comments
 (0)