How to Reduce Code Duplication with “inherit_data”

How to Reduce Code Duplication with “inherit_data”

The inherit_data form field option can be very useful when you have some duplicated fields in different entities. For example, imagine you have two entities, a Company and a Customer:

  1. // src/Entity/Company.php
  2. namespace App\Entity;
  3. class Company
  4. {
  5. private $name;
  6. private $website;
  7. private $address;
  8. private $zipcode;
  9. private $city;
  10. private $country;
  11. }
  1. // src/Entity/Customer.php
  2. namespace App\Entity;
  3. class Customer
  4. {
  5. private $firstName;
  6. private $lastName;
  7. private $address;
  8. private $zipcode;
  9. private $city;
  10. private $country;
  11. }

As you can see, each entity shares a few of the same fields: address, zipcode, city, country.

Start with building two forms for these entities, CompanyType and CustomerType:

  1. // src/Form/Type/CompanyType.php
  2. namespace App\Form\Type;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. class CompanyType extends AbstractType
  7. {
  8. public function buildForm(FormBuilderInterface $builder, array $options): void
  9. {
  10. $builder
  11. ->add('name', TextType::class)
  12. ->add('website', TextType::class);
  13. }
  14. }
  1. // src/Form/Type/CustomerType.php
  2. namespace App\Form\Type;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. class CustomerType extends AbstractType
  7. {
  8. public function buildForm(FormBuilderInterface $builder, array $options): void
  9. {
  10. $builder
  11. ->add('firstName', TextType::class)
  12. ->add('lastName', TextType::class);
  13. }
  14. }

Instead of including the duplicated fields address, zipcode, city and country in both of these forms, create a third form called LocationType for that:

  1. // src/Form/Type/LocationType.php
  2. namespace App\Form\Type;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. class LocationType extends AbstractType
  9. {
  10. public function buildForm(FormBuilderInterface $builder, array $options): void
  11. {
  12. $builder
  13. ->add('address', TextareaType::class)
  14. ->add('zipcode', TextType::class)
  15. ->add('city', TextType::class)
  16. ->add('country', TextType::class);
  17. }
  18. public function configureOptions(OptionsResolver $resolver): void
  19. {
  20. $resolver->setDefaults([
  21. 'inherit_data' => true,
  22. ]);
  23. }
  24. }

The location form has an interesting option set, namely inherit_data. This option lets the form inherit its data from its parent form. If embedded in the company form, the fields of the location form will access the properties of the Company instance. If embedded in the customer form, the fields will access the properties of the Customer instance instead. Convenient, eh?

Note

Instead of setting the inherit_data option inside LocationType, you can also (just like with any option) pass it in the third argument of $builder->add().

Finally, make this work by adding the location form to your two original forms:

  1. // src/Form/Type/CompanyType.php
  2. namespace App\Form\Type;
  3. use App\Entity\Company;
  4. use Symfony\Component\Form\AbstractType;
  5. // ...
  6. class CompanyType extends AbstractType
  7. {
  8. public function buildForm(FormBuilderInterface $builder, array $options): void
  9. {
  10. // ...
  11. $builder->add('foo', LocationType::class, [
  12. 'data_class' => Company::class,
  13. ]);
  14. }
  15. }
  1. // src/Form/Type/CustomerType.php
  2. namespace App\Form\Type;
  3. use App\Entity\Customer;
  4. use Symfony\Component\Form\AbstractType;
  5. class CustomerType extends AbstractType
  6. {
  7. public function buildForm(FormBuilderInterface $builder, array $options): void
  8. {
  9. // ...
  10. $builder->add('bar', LocationType::class, [
  11. 'data_class' => Customer::class,
  12. ]);
  13. }
  14. }

That’s it! You have extracted duplicated field definitions to a separate location form that you can reuse wherever you need it.

Caution

Forms with the inherit_data option set cannot have *_SET_DATA event listeners.

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