Skip to content

Commit 9909728

Browse files
committed
[Form] Deprecated FormTypeInterface::getName() and passing of type instances
1 parent 02ff198 commit 9909728

File tree

108 files changed

+2191
-958
lines changed

Some content is hidden

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

108 files changed

+2191
-958
lines changed

AbstractExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ private function initTypes()
156156
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
157157
}
158158

159-
$this->types[$type->getName()] = $type;
159+
// Since Symfony 3.0 types are identified by their FQCN
160+
$fqcn = get_class($type);
161+
$legacyName = $type->getName();
162+
163+
$this->types[$fqcn] = $type;
164+
165+
if ($legacyName) {
166+
$this->types[$legacyName] = $type;
167+
}
160168
}
161169
}
162170

AbstractType.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form;
1313

14+
use Symfony\Component\Form\Util\StringUtil;
1415
use Symfony\Component\OptionsResolver\OptionsResolver;
1516
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1617

@@ -61,11 +62,37 @@ public function configureOptions(OptionsResolver $resolver)
6162
{
6263
}
6364

65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getName()
69+
{
70+
// As of Symfony 2.8, the name defaults to the fully-qualified class name
71+
return get_class($this);
72+
}
73+
74+
/**
75+
* Returns the prefix of the template block name for this type.
76+
*
77+
* The block prefixes defaults to the underscored short class name with
78+
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
79+
*
80+
* @return string The prefix of the template block name
81+
*/
82+
public function getBlockPrefix()
83+
{
84+
$fqcn = get_class($this);
85+
$name = $this->getName();
86+
87+
// For BC: Use the name as block prefix if one is set
88+
return $name !== $fqcn ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
89+
}
90+
6491
/**
6592
* {@inheritdoc}
6693
*/
6794
public function getParent()
6895
{
69-
return 'form';
96+
return 'Symfony\Component\Form\Extension\Core\Type\FormType';
7097
}
7198
}

Extension/Core/Type/BaseType.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\FormBuilderInterface;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\Form\FormView;
18+
use Symfony\Component\Form\Util\StringUtil;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
/**
@@ -77,7 +78,17 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7778

7879
$blockPrefixes = array();
7980
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
80-
array_unshift($blockPrefixes, $type->getName());
81+
if (method_exists($type, 'getBlockPrefix')) {
82+
array_unshift($blockPrefixes, $type->getBlockPrefix());
83+
} else {
84+
@trigger_error(get_class($type).': The ResolvedFormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should add it to your implementation.', E_USER_DEPRECATED);
85+
86+
$fqcn = get_class($type->getInnerType());
87+
$name = $type->getName();
88+
$hasCustomName = $name !== $fqcn;
89+
90+
array_unshift($blockPrefixes, $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn));
91+
}
8192
}
8293
$blockPrefixes[] = $uniqueBlockPrefix;
8394

Extension/Core/Type/BirthdayType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ public function configureOptions(OptionsResolver $resolver)
3131
*/
3232
public function getParent()
3333
{
34-
return 'date';
34+
return __NAMESPACE__.'\DateType';
3535
}
3636

3737
/**
3838
* {@inheritdoc}
3939
*/
4040
public function getName()
41+
{
42+
return $this->getBlockPrefix();
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getBlockPrefix()
4149
{
4250
return 'birthday';
4351
}

Extension/Core/Type/ButtonType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public function getParent()
3232
* {@inheritdoc}
3333
*/
3434
public function getName()
35+
{
36+
return $this->getBlockPrefix();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getBlockPrefix()
3543
{
3644
return 'button';
3745
}

Extension/Core/Type/CheckboxType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public function configureOptions(OptionsResolver $resolver)
6666
* {@inheritdoc}
6767
*/
6868
public function getName()
69+
{
70+
return $this->getBlockPrefix();
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getBlockPrefix()
6977
{
7078
return 'checkbox';
7179
}

Extension/Core/Type/ChoiceType.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ public function configureOptions(OptionsResolver $resolver)
361361
* {@inheritdoc}
362362
*/
363363
public function getName()
364+
{
365+
return $this->getBlockPrefix();
366+
}
367+
368+
/**
369+
* {@inheritdoc}
370+
*/
371+
public function getBlockPrefix()
364372
{
365373
return 'choice';
366374
}
@@ -424,12 +432,12 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
424432
);
425433

426434
if ($options['multiple']) {
427-
$choiceType = 'checkbox';
435+
$choiceType = __NAMESPACE__.'\CheckboxType';
428436
// The user can check 0 or more checkboxes. If required
429437
// is true, he is required to check all of them.
430438
$choiceOpts['required'] = false;
431439
} else {
432-
$choiceType = 'radio';
440+
$choiceType = __NAMESPACE__.'\RadioType';
433441
}
434442

435443
$builder->add($name, $choiceType, $choiceOpts);

Extension/Core/Type/CollectionType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function configureOptions(OptionsResolver $resolver)
8888
'prototype' => true,
8989
'prototype_data' => null,
9090
'prototype_name' => '__name__',
91-
'type' => 'text',
91+
'type' => __NAMESPACE__.'\TextType',
9292
'options' => array(),
9393
'delete_empty' => false,
9494
));
@@ -100,6 +100,14 @@ public function configureOptions(OptionsResolver $resolver)
100100
* {@inheritdoc}
101101
*/
102102
public function getName()
103+
{
104+
return $this->getBlockPrefix();
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function getBlockPrefix()
103111
{
104112
return 'collection';
105113
}

Extension/Core/Type/CountryType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public function configureOptions(OptionsResolver $resolver)
3333
*/
3434
public function getParent()
3535
{
36-
return 'choice';
36+
return __NAMESPACE__.'\ChoiceType';
3737
}
3838

3939
/**
4040
* {@inheritdoc}
4141
*/
4242
public function getName()
43+
{
44+
return $this->getBlockPrefix();
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function getBlockPrefix()
4351
{
4452
return 'country';
4553
}

Extension/Core/Type/CurrencyType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public function configureOptions(OptionsResolver $resolver)
3333
*/
3434
public function getParent()
3535
{
36-
return 'choice';
36+
return __NAMESPACE__.'\ChoiceType';
3737
}
3838

3939
/**
4040
* {@inheritdoc}
4141
*/
4242
public function getName()
43+
{
44+
return $this->getBlockPrefix();
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function getBlockPrefix()
4351
{
4452
return 'currency';
4553
}

0 commit comments

Comments
 (0)