Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b38d74

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 3c10715 + 3d9e5de commit 5b38d74

File tree

120 files changed

+2705
-1033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2705
-1033
lines changed

UPGRADE-2.8.md

Lines changed: 191 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Form
1212
Before:
1313

1414
```php
15-
$form = $this->createForm('form', $article, array('cascade_validation' => true))
15+
$form = $this->createFormBuilder($article, array('cascade_validation' => true))
1616
->add('author', new AuthorType())
1717
->getForm();
1818
```
@@ -21,8 +21,8 @@ Form
2121

2222
```php
2323
use Symfony\Component\Validator\Constraints\Valid;
24-
25-
$form = $this->createForm('form', $article)
24+
25+
$form = $this->createFormBuilder($article)
2626
->add('author', new AuthorType(), array(
2727
'constraints' => new Valid(),
2828
))
@@ -42,6 +42,194 @@ Form
4242
private $author;
4343
}
4444
```
45+
46+
* Type names were deprecated and will be removed in Symfony 3.0. Instead of
47+
referencing types by name, you should reference them by their
48+
fully-qualified class name (FQCN) instead. With PHP 5.5 or later, you can
49+
use the "class" constant for that:
50+
51+
Before:
52+
53+
```php
54+
$form = $this->createFormBuilder()
55+
->add('name', 'text')
56+
->add('age', 'integer')
57+
->getForm();
58+
```
59+
60+
After:
61+
62+
```php
63+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
64+
use Symfony\Component\Form\Extension\Core\Type\TextType;
65+
66+
$form = $this->createFormBuilder()
67+
->add('name', TextType::class)
68+
->add('age', IntegerType::class)
69+
->getForm();
70+
```
71+
72+
As a further consequence, the method `FormTypeInterface::getName()` was
73+
deprecated and will be removed in Symfony 3.0. You should remove this method
74+
from your form types.
75+
76+
If you want to customize the block prefix of a type in Twig, you should now
77+
implement `FormTypeInterface::getBlockPrefix()` instead:
78+
79+
Before:
80+
81+
```php
82+
class UserProfileType extends AbstractType
83+
{
84+
public function getName()
85+
{
86+
return 'profile';
87+
}
88+
}
89+
```
90+
91+
After:
92+
93+
```php
94+
class UserProfileType extends AbstractType
95+
{
96+
public function getBlockPrefix()
97+
{
98+
return 'profile';
99+
}
100+
}
101+
```
102+
103+
If you don't customize `getBlockPrefix()`, it defaults to the class name
104+
without "Type" suffix in underscore notation (here: "user_profile").
105+
106+
If you want to create types that are compatible with Symfony 2.3 up to 2.8
107+
and don't trigger deprecation errors, implement *both* `getName()` and
108+
`getBlockPrefix()`:
109+
110+
```php
111+
class ProfileType extends AbstractType
112+
{
113+
public function getName()
114+
{
115+
return $this->getBlockPrefix();
116+
}
117+
118+
public function getBlockPrefix()
119+
{
120+
return 'profile';
121+
}
122+
}
123+
```
124+
125+
If you define your form types in the Dependency Injection configuration, you
126+
should further remove the "alias" attribute:
127+
128+
Before:
129+
130+
```xml
131+
<service id="my.type" class="Vendor\Type\MyType">
132+
<tag name="form.type" alias="mytype" />
133+
</service>
134+
```
135+
136+
After:
137+
138+
```xml
139+
<service id="my.type" class="Vendor\Type\MyType">
140+
<tag name="form.type" />
141+
</service>
142+
```
143+
144+
Type extension should return the fully-qualified class name of the extended
145+
type from `FormTypeExtensionInterface::getExtendedType()` now.
146+
147+
Before:
148+
149+
```php
150+
class MyTypeExtension extends AbstractTypeExtension
151+
{
152+
public function getExtendedType()
153+
{
154+
return 'form';
155+
}
156+
}
157+
```
158+
159+
After:
160+
161+
```php
162+
use Symfony\Component\Form\Extension\Core\Type\FormType;
163+
164+
class MyTypeExtension extends AbstractTypeExtension
165+
{
166+
public function getExtendedType()
167+
{
168+
return FormType::class;
169+
}
170+
}
171+
```
172+
173+
If your extension has to be compatible with Symfony 2.3-2.8, use the
174+
following statement:
175+
176+
```php
177+
use Symfony\Component\Form\AbstractType;
178+
use Symfony\Component\Form\Extension\Core\Type\FormType;
179+
180+
class MyTypeExtension extends AbstractTypeExtension
181+
{
182+
public function getExtendedType()
183+
{
184+
method_exists(AbstractType::class, 'getBlockPrefix') ? FormType::class : 'form';
185+
}
186+
}
187+
```
188+
189+
* Returning type instances from `FormTypeInterface::getParent()` is deprecated
190+
and will not be supported anymore in Symfony 3.0. Return the fully-qualified
191+
class name of the parent type class instead.
192+
193+
Before:
194+
195+
```php
196+
class MyType
197+
{
198+
public function getParent()
199+
{
200+
return new ParentType();
201+
}
202+
}
203+
```
204+
205+
After:
206+
207+
```php
208+
class MyType
209+
{
210+
public function getParent()
211+
{
212+
return ParentType::class;
213+
}
214+
}
215+
```
216+
217+
* Passing type instances to `Form::add()`, `FormBuilder::add()` and the
218+
`FormFactory::create*()` methods is deprecated and will not be supported
219+
anymore in Symfony 3.0. Pass the fully-qualified class name of the type
220+
instead.
221+
222+
Before:
223+
224+
```php
225+
$form = $this->createForm(new MyType());
226+
```
227+
228+
After:
229+
230+
```php
231+
$form = $this->createForm(MyType::class);
232+
```
45233

46234
Translator
47235
----------

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public static function createChoiceName($choice, $key, $value)
9494
* Gets important parts from QueryBuilder that will allow to cache its results.
9595
* For instance in ORM two query builders with an equal SQL string and
9696
* equal parameters are considered to be equal.
97-
*
97+
*
9898
* @param object $queryBuilder
99-
*
99+
*
100100
* @return array|false Array with important QueryBuilder parts or false if
101101
* they can't be determined
102-
*
102+
*
103103
* @internal This method is public to be usable as callback. It should not
104104
* be used in user code.
105105
*/
@@ -335,6 +335,6 @@ abstract public function getLoader(ObjectManager $manager, $queryBuilder, $class
335335

336336
public function getParent()
337337
{
338-
return 'choice';
338+
return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
339339
}
340340
}

src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,30 @@ public function getLoader(ObjectManager $manager, $queryBuilder, $class)
5656
return new ORMQueryBuilderLoader($queryBuilder, $manager, $class);
5757
}
5858

59+
/**
60+
* {@inheritdoc}
61+
*/
5962
public function getName()
63+
{
64+
return $this->getBlockPrefix();
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function getBlockPrefix()
6071
{
6172
return 'entity';
6273
}
6374

6475
/**
6576
* We consider two query builders with an equal SQL string and
6677
* equal parameters to be equal.
67-
*
78+
*
6879
* @param QueryBuilder $queryBuilder
69-
*
80+
*
7081
* @return array
71-
*
82+
*
7283
* @internal This method is public to be usable as callback. It should not
7384
* be used in user code.
7485
*/

0 commit comments

Comments
 (0)