phpsymfonysymfony7

Symfony 7 - proper way to reuse parts of common forms with many entities


I need a help with check if my thinking is correct.

My application allows to publish adverts on website. I have have many entites with similar fields. For example consider this approach:

I have 3 entities:

I have two traits (to avoid declare the same entity properties many times): BaseTrait: name, description AddressTrait: address1, address2, city

And I have 2 custom form fields:

My goal is use fields address, and baseInfo to combine it in one field and attach constraints from entity to fields, included in these custom fields, for example:

class Advertisement {
    // My entity
    #[Assert\NotBlank]
    private string $name;

    #[Assert\NotBlank]
    private string $description;

    #[Assert\NotBlank]
    private string $address1;

    #[Assert\NotBlank]
    private string $address2;

    #[Assert\NotBlank]
    private string $city;

//Form 
$builder->add('baseInfo', BaseType::class, [ // Map properties from this field to Advertisement properties: name, description
$builder->add('address', AddressType::class, [ // Map properties from this field to Customer properties: address1, address2, city

So after submit form, I should have errors for fields name, description, address1, address2, city when will be empty (because Entity defines constraints NotBlank).

I would like do the same (reuse some of my custom fields) with other entities, like:

class Customer {
    // My entity
    #[Assert\NotBlank]
    private string $name;

    #[Assert\NotBlank]
    private string $description;

    #[Assert\NotBlank]
    private string $anotherProperty;
//Form 
$builder->add('baseInfo', BaseType::class, [ // Map properties from this field to Customer properties: name, description
$builder->add('anotherProperty', TextType::class

Similar, fields name, description should have constraints from Entity

I tried many things from ChatGPT or Symfony docs, but no luck. When I try to add custom field, I cannot set mapping=>true, because property address/baseInfo doesn't exist inside entity (its logical). When I put mapping=>false, then validation is skiped.

I tried to combine validation groups, but not luck as well. Seems that Symfony docs doesn't say anything how to nest Entity fields into custom form type with custom fields (with constraints).

I didn't share my code here, becasue I'm wondering if my thinking is correct. Maybe I could resolve it by parent Abstract class or make full form based on something Interface etc. But it means, that for (for example 50 entities), I will have to create 50 similar or unique forms, what it's unecessary effort for me.

So in sum, will be grateful for ideas how to proper resolve this issue and don't repeat too much code.

Will be grateful for all your hints.


Solution

  • What you are trying to do is pretty common in Symfony forms, and luckily, there is documentation on this subject. There is a form option called 'inherit_data', that allows you to reduce duplicate code.

    Read more about it on the Symfony website: https://symfony.com/doc/current/form/inherit_data_option.html