How to Create a Form Type Extension

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 Symfony\Component\Form\AbstractTypeExtension (you can implement Symfony\Component\Form\FormTypeExtensionInterface instead if you prefer):

  1. // src/Form/Extension/ImageTypeExtension.php
  2. namespace App\Form\Extension;
  3. use Symfony\Component\Form\AbstractTypeExtension;
  4. use Symfony\Component\Form\Extension\Core\Type\FileType;
  5. class ImageTypeExtension extends AbstractTypeExtension
  6. {
  7. /**
  8. * Return the class of the type being extended.
  9. */
  10. public static function getExtendedTypes(): iterable
  11. {
  12. // return FormType::class to modify (nearly) every field in the system
  13. return [FileType::class];
  14. }
  15. }

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

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.

Tip

Run the following command to verify that the form type extension was successfully registered in the application:

  1. $ php bin/console debug:form

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. // src/Entity/Media.php
  2. namespace App\Entity;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. class Media
  5. {
  6. // ...
  7. /**
  8. * @var string The path - typically stored in the database
  9. */
  10. private $path;
  11. // ...
  12. public function getWebPath(): string
  13. {
  14. // ... $webPath being the full image URL, to be used in templates
  15. return $webPath;
  16. }
  17. }

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. // src/Form/Extension/ImageTypeExtension.php
  2. namespace App\Form\Extension;
  3. use Symfony\Component\Form\AbstractTypeExtension;
  4. use Symfony\Component\Form\Extension\Core\Type\FileType;
  5. use Symfony\Component\Form\FormInterface;
  6. use Symfony\Component\Form\FormView;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. class ImageTypeExtension extends AbstractTypeExtension
  10. {
  11. public static function getExtendedTypes(): iterable
  12. {
  13. // return FormType::class to modify (nearly) every field in the system
  14. return [FileType::class];
  15. }
  16. public function configureOptions(OptionsResolver $resolver): void
  17. {
  18. // makes it legal for FileType fields to have an image_property option
  19. $resolver->setDefined(['image_property']);
  20. }
  21. public function buildView(FormView $view, FormInterface $form, array $options): void
  22. {
  23. if (isset($options['image_property'])) {
  24. // this will be whatever class/entity is bound to your form (e.g. Media)
  25. $parentData = $form->getParent()->getData();
  26. $imageUrl = null;
  27. if (null !== $parentData) {
  28. $accessor = PropertyAccess::createPropertyAccessor();
  29. $imageUrl = $accessor->getValue($parentData, $options['image_property']);
  30. }
  31. // sets an "image_url" variable that will be available when rendering this field
  32. $view->vars['image_url'] = $imageUrl;
  33. }
  34. }
  35. }

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 form fragment naming rules.

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. {# templates/form/fields.html.twig #}
  2. {% extends 'form_div_layout.html.twig' %}
  3. {% block file_widget %}
  4. {{ block('form_widget') }}
  5. {% if image_url is not null %}
  6. <img src="{{ asset(image_url) }}"/>
  7. {% endif %}
  8. {% 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. // src/Form/Type/MediaType.php
  2. namespace App\Form\Type;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\FileType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. class MediaType extends AbstractType
  8. {
  9. public function buildForm(FormBuilderInterface $builder, array $options): void
  10. {
  11. $builder
  12. ->add('name', TextType::class)
  13. ->add('file', FileType::class, ['image_property' => 'webPath']);
  14. }
  15. }

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. // src/Form/Extension/DateTimeExtension.php
  2. namespace App\Form\Extension;
  3. // ...
  4. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  5. use Symfony\Component\Form\Extension\Core\Type\DateType;
  6. use Symfony\Component\Form\Extension\Core\Type\TimeType;
  7. class DateTimeExtension extends AbstractTypeExtension
  8. {
  9. // ...
  10. public static function getExtendedTypes(): iterable
  11. {
  12. return [DateTimeType::class, DateType::class, TimeType::class];
  13. }
  14. }

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