Skip to content

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

@dt-justin

Description

@dt-justin

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions