Skip to content

Commit

Permalink
[Form] Deprecated FormTypeInterface::getName() and passing of type in…
Browse files Browse the repository at this point in the history
…stances
  • Loading branch information
webmozart committed Jul 2, 2015
1 parent 8bd89dc commit 3b14634
Show file tree
Hide file tree
Showing 108 changed files with 2,191 additions and 958 deletions.
10 changes: 9 additions & 1 deletion AbstractExtension.php
Expand Up @@ -156,7 +156,15 @@ private function initTypes()
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
}

$this->types[$type->getName()] = $type;
// Since Symfony 3.0 types are identified by their FQCN
$fqcn = get_class($type);
$legacyName = $type->getName();

$this->types[$fqcn] = $type;

if ($legacyName) {
$this->types[$legacyName] = $type;
}
}
}

Expand Down
29 changes: 28 additions & 1 deletion AbstractType.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form;

use Symfony\Component\Form\Util\StringUtil;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Expand Down Expand Up @@ -61,11 +62,37 @@ public function configureOptions(OptionsResolver $resolver)
{
}

/**
* {@inheritdoc}
*/
public function getName()
{
// As of Symfony 2.8, the name defaults to the fully-qualified class name
return get_class($this);
}

/**
* Returns the prefix of the template block name for this type.
*
* The block prefixes defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* @return string The prefix of the template block name
*/
public function getBlockPrefix()
{
$fqcn = get_class($this);
$name = $this->getName();

// For BC: Use the name as block prefix if one is set
return $name !== $fqcn ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'form';
return 'Symfony\Component\Form\Extension\Core\Type\FormType';
}
}
13 changes: 12 additions & 1 deletion Extension/Core/Type/BaseType.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Util\StringUtil;
use Symfony\Component\OptionsResolver\OptionsResolver;

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

$blockPrefixes = array();
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
array_unshift($blockPrefixes, $type->getName());
if (method_exists($type, 'getBlockPrefix')) {
array_unshift($blockPrefixes, $type->getBlockPrefix());
} else {
@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);

$fqcn = get_class($type->getInnerType());
$name = $type->getName();
$hasCustomName = $name !== $fqcn;

array_unshift($blockPrefixes, $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn));
}
}
$blockPrefixes[] = $uniqueBlockPrefix;

Expand Down
10 changes: 9 additions & 1 deletion Extension/Core/Type/BirthdayType.php
Expand Up @@ -31,13 +31,21 @@ public function configureOptions(OptionsResolver $resolver)
*/
public function getParent()
{
return 'date';
return __NAMESPACE__.'\DateType';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'birthday';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/ButtonType.php
Expand Up @@ -32,6 +32,14 @@ public function getParent()
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'button';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/CheckboxType.php
Expand Up @@ -66,6 +66,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'checkbox';
}
Expand Down
12 changes: 10 additions & 2 deletions Extension/Core/Type/ChoiceType.php
Expand Up @@ -361,6 +361,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'choice';
}
Expand Down Expand Up @@ -424,12 +432,12 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
);

if ($options['multiple']) {
$choiceType = 'checkbox';
$choiceType = __NAMESPACE__.'\CheckboxType';
// The user can check 0 or more checkboxes. If required
// is true, he is required to check all of them.
$choiceOpts['required'] = false;
} else {
$choiceType = 'radio';
$choiceType = __NAMESPACE__.'\RadioType';
}

$builder->add($name, $choiceType, $choiceOpts);
Expand Down
10 changes: 9 additions & 1 deletion Extension/Core/Type/CollectionType.php
Expand Up @@ -88,7 +88,7 @@ public function configureOptions(OptionsResolver $resolver)
'prototype' => true,
'prototype_data' => null,
'prototype_name' => '__name__',
'type' => 'text',
'type' => __NAMESPACE__.'\TextType',
'options' => array(),
'delete_empty' => false,
));
Expand All @@ -100,6 +100,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'collection';
}
Expand Down
10 changes: 9 additions & 1 deletion Extension/Core/Type/CountryType.php
Expand Up @@ -33,13 +33,21 @@ public function configureOptions(OptionsResolver $resolver)
*/
public function getParent()
{
return 'choice';
return __NAMESPACE__.'\ChoiceType';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'country';
}
Expand Down
10 changes: 9 additions & 1 deletion Extension/Core/Type/CurrencyType.php
Expand Up @@ -33,13 +33,21 @@ public function configureOptions(OptionsResolver $resolver)
*/
public function getParent()
{
return 'choice';
return __NAMESPACE__.'\ChoiceType';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'currency';
}
Expand Down
12 changes: 10 additions & 2 deletions Extension/Core/Type/DateTimeType.php
Expand Up @@ -161,8 +161,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'time' => $timeParts,
)),
)))
->add('date', 'date', $dateOptions)
->add('time', 'time', $timeOptions)
->add('date', __NAMESPACE__.'\DateType', $dateOptions)
->add('time', __NAMESPACE__.'\TimeType', $timeOptions)
;
}

Expand Down Expand Up @@ -284,6 +284,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'datetime';
}
Expand Down
19 changes: 16 additions & 3 deletions Extension/Core/Type/DateType.php
Expand Up @@ -37,6 +37,11 @@ class DateType extends AbstractType
\IntlDateFormatter::SHORT,
);

private static $widgets = array(
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
);

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -101,9 +106,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

$builder
->add('year', $options['widget'], $yearOptions)
->add('month', $options['widget'], $monthOptions)
->add('day', $options['widget'], $dayOptions)
->add('year', self::$widgets[$options['widget']], $yearOptions)
->add('month', self::$widgets[$options['widget']], $monthOptions)
->add('day', self::$widgets[$options['widget']], $dayOptions)
->addViewTransformer(new DateTimeToArrayTransformer(
$options['model_timezone'], $options['view_timezone'], array('year', 'month', 'day')
))
Expand Down Expand Up @@ -253,6 +258,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'date';
}
Expand Down
10 changes: 9 additions & 1 deletion Extension/Core/Type/EmailType.php
Expand Up @@ -20,13 +20,21 @@ class EmailType extends AbstractType
*/
public function getParent()
{
return 'text';
return __NAMESPACE__.'\TextType';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'email';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/FileType.php
Expand Up @@ -61,6 +61,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'file';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/FormType.php
Expand Up @@ -244,6 +244,14 @@ public function getParent()
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'form';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/HiddenType.php
Expand Up @@ -34,6 +34,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'hidden';
}
Expand Down
8 changes: 8 additions & 0 deletions Extension/Core/Type/IntegerType.php
Expand Up @@ -73,6 +73,14 @@ public function configureOptions(OptionsResolver $resolver)
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'integer';
}
Expand Down

0 comments on commit 3b14634

Please sign in to comment.