Symfony 2.8 Preps for Big Changes in Form Building Coming with Version 3.0

Screen Shot 2015-11-27 at 9.31.15 AM In the next major version of Symfony, version 3, there are lots of changes to the form building. These changes will require you to modify all of your existing form building code.

The core of the change is the requirement that you use fully qualified class name when referencing the form element types. This change ripples throughout the entire form building process.

Here’s how we’re currently doing it.

$form = $this->createFormBuilder()
   ->add('president', 'text')
   ->add('age', 'integer')
   ->getForm();

Now you have to do it like this.

use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

$form = $this->createFormBuilder()
   ->add('president', TextType::class)
   ->add('age', IntegerType::class)
   ->getForm();

The getName() method is also going away.

So instead of this.

class PresidentType extends AbstractType
{
   public function getName()
   {
       return 'president';
   }
}

You now should do this.

class PresidentType extends AbstractType
{
   public function getBlockPrefix()
   {
       return 'president';
   }
}

I’m not sure why the change is necessary, I guess it’s a better implementation. But if you have code that needs to run on all versions 3 and 2.3 to 2.8, you have to implement both getBlockPrefix and getName.

class PresidentType extends AbstractType
{
   public function getName()
   {
       return $this->getBlockPrefix();
   }

   public function getBlockPrefix()
   {
       return 'president';
   }
}

Also, if you define form types using a Dependency Injection configuration, you need to remove the “alias” attribute.

This is what it used to look like.

<service id="my.type" class="Vendor\Type\MyType">
   <tag name="form.type" alias="mytype" />
</service>

Now you need to do this.

<service id="my.type" class="Vendor\Type\MyType">
   <tag name="form.type" />
</service>

The FormTypeExtensionInterface::getExtendedType() is different now too.

You used to be able to do this.

class MyTypeExtension extends AbstractTypeExtension
{
   public function getExtendedType()
   {
       return 'form';
   }
}

Now you have to do this.

use Symfony\Component\Form\Extension\Core\Type\FormType;

class MyTypeExtension extends AbstractTypeExtension
{
   public function getExtendedType()
   {
       return FormType::class;
   }
}

If you use FormTypeInterface::getParent(), that’s deprecated and will not be supported anymore in Symfony 3.0. Return the fully-qualified class name of the parent type class instead.

You might currently be doing this.

class MyType
{
   public function getParent()
   {
       return new ParentType();
   }
}

Now you have to do this.

class MyType
{
   public function getParent()
   {
       return ParentType::class;
   }
}

You will now need to use the fully qualified class name when creating forms too.

Passing type instances to Form::add(), FormBuilder::add() and the FormFactory::create*() methods is deprecated and will not be supported anymore in Symfony 3.0. Pass the fully-qualified class name of the type instead.

This

$form = $this->createForm(new MyType());

Is now this

$form = $this->createForm(MyType::class);

These changes will require code modifications to move up to version 3.0. I believe that you can start implementing these changes with version 2.8 so that your code is ready when 3.0 goes live.

Check out all of the changes introduced in Symfony version 2.8.

Screen shot 2015-11-22 at 8.53.10 PM
Read my Symfony Getting Started eBook on Sale on Amazon $9

Share Button

2 Comments

Bernhard

You don’t need to implement getBlockPrefix(). There’s a default implementation that returns a prefix based on your class name.

Comments are closed.