How to Configure empty Data for a Form Class

How to Configure empty Data for a Form Class

The empty_data option allows you to specify an empty data set for your form class. This empty data set would be used if you submit your form, but haven’t called setData() on your form or passed in data when you created your form. For example, in a controller:

  1. public function index(): Response
  2. {
  3. $blog = ...;
  4. // $blog is passed in as the data, so the empty_data
  5. // option is not needed
  6. $form = $this->createForm(BlogType::class, $blog);
  7. // no data is passed in, so empty_data is
  8. // used to get the "starting data"
  9. $form = $this->createForm(BlogType::class);
  10. }

By default, empty_data is set to null. Or, if you have specified a data_class option for your form class, it will default to a new instance of that class. That instance will be created by calling the constructor with no arguments.

If you want to override this default behavior, there are two ways to do this:

If you didn’t set the data_class option, you can pass the initial data as string or pass an array of strings (where the key matches the field name) when the form type is compound.

Option 1: Instantiate a new Class

One reason you might use this option is if you want to use a constructor that takes arguments. Remember, the default data_class option calls that constructor with no arguments:

  1. // src/Form/Type/BlogType.php
  2. namespace App\Form\Type;
  3. // ...
  4. use App\Entity\Blog;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\OptionsResolver\OptionsResolver;
  7. class BlogType extends AbstractType
  8. {
  9. private $someDependency;
  10. public function __construct($someDependency)
  11. {
  12. $this->someDependency = $someDependency;
  13. }
  14. // ...
  15. public function configureOptions(OptionsResolver $resolver): void
  16. {
  17. $resolver->setDefaults([
  18. 'empty_data' => new Blog($this->someDependency),
  19. ]);
  20. }
  21. }

You can instantiate your class however you want. In this example, you pass some dependency into the BlogType then use that to instantiate the Blog class. The point is, you can set empty_data to the exact “new” object that you want to use.

Tip

In order to pass arguments to the BlogType constructor, you’ll need to register the form as a service and tag it with form.type. If you’re using the default services.yaml configuration, this is already done for you.

Option 2: Provide a Closure

Using a closure is the preferred method, since it will only create the object if it is needed.

The closure must accept a FormInterface instance as the first argument:

  1. use Symfony\Component\Form\FormInterface;
  2. use Symfony\Component\OptionsResolver\OptionsResolver;
  3. // ...
  4. public function configureOptions(OptionsResolver $resolver): void
  5. {
  6. $resolver->setDefaults([
  7. 'empty_data' => function (FormInterface $form) {
  8. return new Blog($form->get('title')->getData());
  9. },
  10. ]);
  11. }

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