EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

Dynamic Form Fields #6260

Open rafaelmartinelli opened 1 month ago

rafaelmartinelli commented 1 month ago

Hello,

I want to generate a form dynamically based on field descriptions in an array. Let's say an event with several participants, and the user may create some dynamic fields for each event. I am using an EAV Model with two classes FormFieldDescription which described the fields for each event, and FormFieldValue, which stores the value for each event and participant:

#[ORM\Entity(repositoryClass: EventRepository::class)]
class Event
{
    //...

    /** @var FormFieldDescription[] $fields */
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: FormFieldDescription::class, orphanRemoval: true)]
    private Collection $fields;

    //...
}

#[ORM\Entity(repositoryClass: ParticipantRepository::class)]
class Participant
{
    //...
    #[ORM\ManyToOne(inversedBy: 'participants')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Event $event = null;

    #[ORM\OneToMany(mappedBy: 'participant', targetEntity: FormFieldValue::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $values;

    public __construct(Event $event)
    {
        $this->event = $event;
        foreach ($this->event->getFields() as $field)
            $this->addValue(new FormFieldValue($field, $this));
    }
    //...
}

When the user creates a participant, I want EA to display a form with the static fields, and the dynamic fields. I managed to display the dynamic fields using a CollectionField, however it looks ugly:

image

I would like to know a way to display them like regular fields, being able to configure them based on FormFieldDescription. Is it possible?