Search code examples
crudsymfony5easyadmin3

EasyAdmin 3 Symfony 5 CrudController AssociationField


I'm using EasyAmdin 3 with Symfony 5 and I have a OneToMany relation between Challenge and Encadrement. Defined In Challenge.php:

/**
 * @ORM\OneToMany(targetEntity=Encadrement::class, mappedBy="challengePartner")
 */
private $bornes;

I made a CRUDController for Challenge, and I want to be able to add Encadrement directly when creating/editing a Challenge. I did this :

        AssociationField::new('bornes'),

I can choose between all the Encadrement already created. But what I want is to be able to multiple add Encadrement and I can't find how to do this. I tried making my own EncadrementType :

class EncadrementType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, array(
                "label" => "Nom"
            ))
            ->add('low', IntegerType::class, array(
                "label" => "Borne basse"
            ))
            ->add('high', IntegerType::class, array(
                "label" => "Borne haute"
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Encadrement::class,
        ]);
    }
}

And something like this in the ChallengeCRUD:

        AssociationField::new('bornes')->setFormType(EncadrementType::class),

But I get this error when I don't even use those options:

An error has occurred resolving the options of the form "App\Form\EncadrementType": The options "class", "multiple", "query_builder" do not exist. Defined options are: "action", "allow_extra_fields", "allow_file_upload", "attr", "attr_translation_parameters", "auto_initialize", "block_name", "block_prefix", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "ea_crud_form", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "getter", "help", "help_attr", "help_html", "help_translation_parameters", "inherit_data", "invalid_message", "invalid_message_parameters", "is_empty_callback", "label", "label_attr", "label_format", "label_html", "label_translation_parameters", "legacy_error_messages", "mapped", "method", "post_max_size_message", "property_path", "required", "row_attr", "setter", "translation_domain", "trim", "upload_max_size_message", "validation_groups".

I tried adding the multiple option to the AssociationField but it does nothing:

        AssociationField::new('bornes')->setFormTypeOption("multiple","true"),

I'm stuck there, thanks for any help !


Solution

  • Try CollectionField like this:

    <?php
    yield CollectionField::new('bornes')
        ->setFormTypeOptions([
            'delete_empty' => true,
            'by_reference' => false,
        ])
        ->setEntryIsComplex(false)
        ->setCustomOptions([
            'allowAdd' => true,
            'allowDelete' => true,
            'entryType' => EncadrementType::class,
            'showEntryLabel' => false,
        ])
        ;