-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
FormConfigBuilder::resetViewTransformers() broken for ChoiceType - Cannot add dynamic options anymore #42451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey, thanks for your report! |
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! |
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, 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, |
@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 ? @ |
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:But now I get
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.
The text was updated successfully, but these errors were encountered: