-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
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
Labels
No labels