EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Example $form->add for a collection containing an association

Open dt-justin opened this issue 4 months ago • 1 comments

I'm using CRUD that has a CollectionField that uses ->useEntryCrudForm(), in the embedded CRUD I has an associationField that I use javascript to populate which is working perfectly. The issue is that I need to be able to add a Listener for PRE_SUBMIT to be able to inject the choice before saving/creating.

I have used this method many times on a direct CRUD, but with an embedded I can't use this style of injection

        $formBuilder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $data = $event->getData();
                $form = $event->getForm();

                if (in_array('typeSelector', array_keys($data))) {
                    return;
                }

                $fields = [
                    'optionUnit' => [
                        'dependsOn' => 'food',
                        'class' => \App\Entity\CNFConversionFactor::class,
                    ],
                ];

                foreach ($fields as $field => $field_options) {
                    if (!in_array($field, array_keys($data))) {
                        continue;
                    }
                    $isDirty = false;
                    $choices = $form->get($field)->getConfig()->getOption('choices');
                    if (!empty($choices)) {
                        $submittedOpts = $data[$field];
                        if (!is_array($submittedOpts)) {
                            $submittedOpts = [$submittedOpts];
                        }
                        foreach ($submittedOpts as $opt) {
                            if (!\in_array($opt, $choices, true)) {
                                $choices[$opt] = $opt;
                                $isDirty = true;
                            }
                        }
                    } else {
                        $isDirty = true;
                    }

                    if ($isDirty) {
                        $value = $data[$field];
                        $form->add($field, FieldType\EntityType::class, [
                            'class' => $field_options['class'],
                            'query_builder' => function ($repo) use ($value) {
                                return $repo->createQueryBuilder('entity')
                                    ->andWhere('entity.id = :value')
                                    ->setParameter('value', $value)
                                ;
                            },
                        ]);
                    }    
                }
            }
        );

        return $formBuilder;
    }

I can see the data, $event->getData() has $data['mealLines'][0]['optionUnit'] but I can not use form->add to update the select in the collection, I'm not sure how to for the $field, I tried using mealLines_0_optionUnit but that doesn't work. is there a way to update the select in the Collection form?

dt-justin avatar Sep 29 '25 21:09 dt-justin

likely do to https://github.com/EasyCorp/EasyAdminBundle/issues/6991 is part of the issue here, I didn't know it was suppose to, if it was I could move the PRE_SUBMIT code to the ->useEntryCrudForm() CRUD instead where I'd have access to each row of the collection.

dt-justin avatar Sep 29 '25 21:09 dt-justin