How to Embed Forms

How to Embed Forms

Often, you’ll want to build a form that will include fields from many different objects. For example, a registration form may contain data belonging to a User object as well as many Address objects. Fortunately this can be achieved by the Form component.

Embedding a Single Object

Suppose that each Task belongs to a Category object. Start by creating the Category object:

  1. // src/Entity/Category.php
  2. namespace App\Entity;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. class Category
  5. {
  6. /**
  7. * @Assert\NotBlank
  8. */
  9. public $name;
  10. }

Next, add a new category property to the Task class:

  1. // ...
  2. class Task
  3. {
  4. // ...
  5. /**
  6. * @Assert\Type(type="App\Entity\Category")
  7. * @Assert\Valid
  8. */
  9. protected $category;
  10. // ...
  11. public function getCategory(): ?Category
  12. {
  13. return $this->category;
  14. }
  15. public function setCategory(?Category $category)
  16. {
  17. $this->category = $category;
  18. }
  19. }

Tip

The Valid Constraint has been added to the property category. This cascades the validation to the corresponding entity. If you omit this constraint, the child entity would not be validated.

Now that your application has been updated to reflect the new requirements, create a form class so that a Category object can be modified by the user:

  1. // src/Form/CategoryType.php
  2. namespace App\Form;
  3. use App\Entity\Category;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\OptionsResolver\OptionsResolver;
  7. class CategoryType extends AbstractType
  8. {
  9. public function buildForm(FormBuilderInterface $builder, array $options): void
  10. {
  11. $builder->add('name');
  12. }
  13. public function configureOptions(OptionsResolver $resolver): void
  14. {
  15. $resolver->setDefaults([
  16. 'data_class' => Category::class,
  17. ]);
  18. }
  19. }

The end goal is to allow the Category of a Task to be modified right inside the task form itself. To accomplish this, add a category field to the TaskType object whose type is an instance of the new CategoryType class:

  1. use App\Form\CategoryType;
  2. use Symfony\Component\Form\FormBuilderInterface;
  3. public function buildForm(FormBuilderInterface $builder, array $options): void
  4. {
  5. // ...
  6. $builder->add('category', CategoryType::class);
  7. }

The fields from CategoryType can now be rendered alongside those from the TaskType class.

Render the Category fields in the same way as the original Task fields:

  1. {# ... #}
  2. <h3>Category</h3>
  3. <div class="category">
  4. {{ form_row(form.category.name) }}
  5. </div>
  6. {# ... #}

When the user submits the form, the submitted data for the Category fields are used to construct an instance of Category, which is then set on the category field of the Task instance.

The Category instance is accessible naturally via $task->getCategory() and can be persisted to the database or used however you need.

Embedding a Collection of Forms

You can also embed a collection of forms into one form (imagine a Category form with many Product sub-forms). This is done by using the collection field type.

For more information see the How to Embed a Collection of Forms article and the CollectionType reference.

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