You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Create a Form Type Extension

4.2 version
Maintained Unmaintained

How to Create a Form Type Extension

Form type extensions are incredibly powerful: they allow you to modify any existing form field types across the entire system.

They have 2 main use-cases:

  1. You want to add a specific feature to a single form type (such as adding a "download" feature to the FileType field type);
  2. You want to add a generic feature to several types (such as adding a "help" text to every "input text"-like type).

Imagine that you have a Media entity, and that each media is associated to a file. Your Media form uses a file type, but when editing the entity, you would like to see its image automatically rendered next to the file input.

Defining the Form Type Extension

First, create the form type extension class extending from AbstractTypeExtension (you can implement FormTypeExtensionInterface instead if you prefer):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// src/Form/Extension/ImageTypeExtension.php
namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class ImageTypeExtension extends AbstractTypeExtension
{
    /**
     * Return the class of the type being extended.
     */
    public static function getExtendedTypes(): iterable
    {
        // return FormType::class to modify (nearly) every field in the system
        return array(FileType::class);
    }
}

The only method you must implement is getExtendedTypes(), which is used to configure which field types you want to modify.

New in version 4.2: The getExtendedTypes() method was introduced in Symfony 4.2.

Depending on your use case, you may need to override some of the following methods:

  • buildForm()
  • buildView()
  • configureOptions()
  • finishView()

For more information on what those methods do, see the custom form field type article.

Registering your Form Type Extension as a Service

Form type extensions must be registered as services and tagged with the form.type_extension tag. If you're using the default services.yaml configuration, this is already done for you, thanks to autoconfiguration.

Tip

There is an optional tag attribute called priority, which defaults to 0 and controls the order in which the form type extensions are loaded (the higher the priority, the earlier an extension is loaded). This is useful when you need to guarantee that one extension is loaded before or after another extension. Using this attribute requires you to add the service configuration explicitly.

Once the extension is registered, any method that you've overridden (e.g. buildForm()) will be called whenever any field of the given type (FileType) is built.

Adding the extension Business Logic

The goal of your extension is to display a nice image next to file input (when the underlying model contains images). For that purpose, suppose that you use an approach similar to the one described in How to handle File Uploads with Doctrine: you have a Media model with a path property, corresponding to the image path in the database:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// src/Entity/Media.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Media
{
    // ...

    /**
     * @var string The path - typically stored in the database
     */
    private $path;

    // ...

    public function getWebPath()
    {
        // ... $webPath being the full image URL, to be used in templates

        return $webPath;
    }
}

Your form type extension class will need to do two things in order to extend the FileType::class form type:

  1. Override the configureOptions() method so that any FileType field can have an image_property option;
  2. Override the buildView() methods to pass the image URL to the view.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// src/Form/Extension/ImageTypeExtension.php
namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class ImageTypeExtension extends AbstractTypeExtension
{
    public static function getExtendedTypes(): iterable
    {
        // return FormType::class to modify (nearly) every field in the system
        return array(FileType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        // makes it legal for FileType fields to have an image_property option
        $resolver->setDefined(array('image_property'));
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        if (isset($options['image_property'])) {
            // this will be whatever class/entity is bound to your form (e.g. Media)
            $parentData = $form->getParent()->getData();

            $imageUrl = null;
            if (null !== $parentData) {
                $accessor = PropertyAccess::createPropertyAccessor();
                $imageUrl = $accessor->getValue($parentData, $options['image_property']);
            }

            // sets an "image_url" variable that will be available when rendering this field
            $view->vars['image_url'] = $imageUrl;
        }
    }

}

Override the File Widget Template Fragment

Each field type is rendered by a template fragment. Those template fragments can be overridden in order to customize form rendering. For more information, you can refer to the What are Form Themes? article.

In your extension class, you added a new variable (image_url), but you still need to take advantage of this new variable in your templates. Specifically, you need to override the file_widget block:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{# templates/form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{% block file_widget %}
    {% spaceless %}

    {{ block('form_widget') }}
    {% if image_url is not null %}
        <img src="{{ asset(image_url) }}"/>
    {% endif %}

    {% endspaceless %}
{% endblock %}

Be sure to configure this form theme template so that the form system sees it.

Using the Form Type Extension

From now on, when adding a field of type FileType::class to your form, you can specify an image_property option that will be used to display an image next to the file field. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// src/Form/Type/MediaType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class MediaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('file', FileType::class, array('image_property' => 'webPath'));
    }
}

When displaying the form, if the underlying model has already been associated with an image, you will see it displayed next to the file input.

Generic Form Type Extensions

You can modify several form types at once by specifying their common parent (Form Types Reference). For example, several form types inherit from the TextType form type (such as EmailType, SearchType, UrlType, etc.). A form type extension applying to TextType (i.e. whose getExtendedType() method returns TextType::class) would apply to all of these form types.

In the same way, since most form types natively available in Symfony inherit from the FormType form type, a form type extension applying to FormType would apply to all of these (notable exceptions are the ButtonType form types). Also keep in mind that if you created (or are using) a custom form type, it's possible that it does not extend FormType, and so your form type extension may not be applied to it.

Another option is to return multiple form types in the getExtendedTypes() method to extend all of them:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// ...
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;

class DateTimeExtension extends AbstractTypeExtension
{
    // ...

    public static function getExtendedTypes(): iterable
    {
        return array(DateTimeType::class, DateType::class, TimeType::class);
    }
}

New in version 4.2: The feature to extend multiple form types using a single extension class was introduced in Symfony 4.2.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.