10000 FormConfigBuilder::resetViewTransformers() broken for ChoiceType - Cannot add dynamic options anymore · Issue #42451 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
jsfgreen opened this issue Aug 9, 2021 · 12 comments

Comments

@jsfgreen
Copy link
jsfgreen commented Aug 9, 2021

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.

@jsfgreen jsfgreen added the Bug label Aug 9, 2021
@jsfgreen jsfgreen changed the title Cannot add dynamic options to ChoiceType field anymore - resetViewTransformers() no longer works FormConfigBuilder::resetViewTransformers() broken for ChoiceType - Cannot add dynamic options anymore Aug 16, 2021
@carsonbot
Copy link

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?

@jsfgreen
Copy link
Author
jsfgreen commented Feb 23, 2022

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])
        }
    })
;

@carsonbot carsonbot removed the Stalled label Feb 23, 2022
@carsonbot
Copy link

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?

@carsonbot
Copy link

Could I get a reply or should I close this?

@carsonbot
Copy link

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!

@ElGittoJunior
Copy link

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?

@jsfgreen
Copy link
Author
jsfgreen commented Feb 8, 2023

@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).

@SimonHassen
Copy link

Hi @carsonbot,
we have the same problem.
Thanks

@ElGittoJunior
Copy link

@jsfgreen That's a great Idea, I used your workaround and it just worked fine. Thanks

@aanair08
Copy link

@carsonbot Yes we have the same problem while upgrading 5.4. Should I need to open a new issue?

@ksn135
Copy link
ksn135 commented Aug 11, 2023

@carsonbot, same problem, sf 6.3

@peteracombina
Copy link

@carsonbot , thanks for the solution. Took me 2 days. Can you share the abstract form class that you mentioned ? @

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
0