Crud - how to configure a custom form in collectionfield for edit

Viewed 880

I need help for configure a collectionField on a crudController.

I have 3 entities: wheel (1,n) - (0,1) wheelsOffers (0,1) - (1,n) Offer. WheelsOffers had few property. I want to add a collectionField to my wheel crud edit form for manage many offers by wheel with few relational properties. I need a custom formType: wheelsOffersType.

In wheelCrudController:

public function configureFields(string $pageName): iterable
{
    return [
        TextField::new('name'),
        TextEditorField::new('description'),
        CollectionField::new('wheelsOffers', 'Offres')
            ->onlyOnForms()
            ->setFormType(WheelsOffersType::class)
    ];
}

In WheelsOffersType:

class WheelsOffersType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('offer', EntityType::class)
        ;
    }

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

}

in entity Wheel:

/**
     * @ORM\OneToMany(targetEntity=WheelsOffers::class, mappedBy="wheel")
     */
    private $wheelsOffers;

in entity WheelsOffers:

    /**
     * @ORM\ManyToOne(targetEntity=Wheel::class, inversedBy="wheelsOffers")
     */
    private $wheel;

    /**
     * @ORM\ManyToOne(targetEntity=Offer::class, inversedBy="wheelsOffers")
     */
    private $offer;

in entity Offer:

/**
     * @ORM\OneToMany(targetEntity=WheelsOffers::class, mappedBy="offer")
     */
    private $wheelsOffers;

But actually I'm blocked by a undefinedOptionsException which I don't understand:

An error has occurred resolving the options of the form "App\Form\WheelsOffersType": The options "allow_add", "allow_delete", "delete_empty", "entry_options" 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", "documentation", "ea_crud_form", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "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", "mapped", "method", "post_max_size_message", "property_path", "required", "row_attr", "translation_domain", "trim", "upload_max_size_message", "validation_groups".

I've tried to use:

1.

CollectionFIeld->setEntryType(WheelsOffers::class),

in place of setFormType but i think it's a wrong way

  1. I set setFormTypeOptions
->setFormType(WheelsOffersType::class)
                ->setFormTypeOptions([
                    'allow_add'=>false,
                    'allow_delete'=>false,
                    'delete_empty'=>false,
                    'entry_options'=>false
                ]),

same result quoted before?

3.

CollectionFIeld->setEntryType(WheelsOffersType::class),

in place of setFormType but I think it's a wrong way too

How can I configure my collectionField?

1 Answers

The answer: in CrudController

public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('name'),
               ...
            CollectionField::new('wheelsOffers', 'Offres')
                ->onlyOnForms()
                ->setEntryType(WheelsOffersType::class),
        ];
    }

in WheelOfferType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('offer', EntityType::class, ['class'=>Offer::class])
            ->add('stock', IntegerType::class)
              ...
        ;
    }

in entities wheel and offer:

/**
     * @ORM\OneToMany(targetEntity=WheelsOffers::class, mappedBy="wheel", cascade={"persist"}, orphanRemoval=true)
     */
    private $wheelsOffers;

thanks to everyone who bothered to watch.

Related