FormConfigBuilder::resetViewTransformers() broken for ChoiceType - Cannot add dynamic options anymore
Symfony version(s) affected: 4.4.20+
Description
Since bug #39659 was addressed in 4.4.20, a FormEvents::POST_SUBMIT listener was added to choice types with 'multiple' => true. This is causing the dynamic options that were added to be flagged as invalid and I cannot submit my form anymore. I am using JavaScript to dynamically add options which used to work just by calling:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// ...
$builder->get('my_field')->resetViewTransformers(); // allow dynamic options
}
But now I get
Symfony\Component\Form\Exception\TransformationFailedException {#2900 ▼
-invalidMessage: null
-invalidMessageParameters: []
#message: "The choices "choice1" do not exist in the choice list."
#code: 0
#file: "....vendor/symfony/form/Extension/Core/Type/ChoiceType.php"
#line: 186
trace: {▶}
}
How to reproduce
Use JavaScript to dynamically add an <option> to your ChoiceType field (select dropdown) and submit.
Additional context I've been stuck at 4.4.19 for a while now because of this. I thought it might get addressed in future releases which is why I waited to submit this bug.
Hey, thanks for your report! There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?
It's relevant but I did find a workaround. And it's the only thing that worked:
$builder
->add('my_field', ChoiceType::class, [
'choices' => ['Choice1' => 'choice1', 'Choice2' => 'choice2']
])
->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$isDirty = false;
$choices = $form->get('my_field')->getConfig()->getOption('choices');
$submittedOpts = $data[$field];
foreach ($submittedOpts as $opt) {
if (!\in_array($opt, $choices, true)) {
$choices[$opt] = $opt;
$isDirty = true;
}
}
if ($isDirty) {
// re-add the same field in the pre-submit so that the newly inserted choices are considered valid
$form->add('my_field', ChoiceType::class, ['choices' => $choices])
}
})
;
Hey, thanks for your report! There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?
Could I get a reply or should I close this?
Hey,
I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!
Hi, I just upgraded some Legacy App to Symfony 5.4 and have the same problem with resetViewTransformers() not working as it used to. Getting the exact same Problem as posted above.
Is there an other way yet or do I have to use the workaround posted by jsfgreen?
@carsonbot Sorry for my delayed response, but no I haven't found a workaround yet and I am still using the same posted solution from Feb 23, 2022. I created an abstract form class to handle this a little more easily and with less bloat in my form types (I use this a lot).
Hi @carsonbot, we have the same problem. Thanks
@jsfgreen That's a great Idea, I used your workaround and it just worked fine. Thanks
@carsonbot Yes we have the same problem while upgrading 5.4. Should I need to open a new issue?
@carsonbot, same problem, sf 6.3
@carsonbot , thanks for the solution. Took me 2 days. Can you share the abstract form class that you mentioned ? @