symfony icon indicating copy to clipboard operation
symfony copied to clipboard

FormConfigBuilder::resetViewTransformers() broken for ChoiceType - Cannot add dynamic options anymore

Open jsfgreen opened this issue 4 years ago • 4 comments

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 avatar Aug 09 '21 13:08 jsfgreen

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 avatar Feb 17 '22 13:02 carsonbot

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

jsfgreen avatar Feb 23 '22 18:02 jsfgreen

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 avatar Aug 28 '22 14:08 carsonbot

Could I get a reply or should I close this?

carsonbot avatar Sep 11 '22 14:09 carsonbot

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!

carsonbot avatar Oct 29 '22 14:10 carsonbot

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?

ElGittoJunior avatar Feb 08 '23 13:02 ElGittoJunior

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

jsfgreen avatar Feb 08 '23 15:02 jsfgreen

Hi @carsonbot, we have the same problem. Thanks

SimonHassen avatar Feb 22 '23 10:02 SimonHassen

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

ElGittoJunior avatar Feb 22 '23 13:02 ElGittoJunior

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

aanair08 avatar Jul 11 '23 12:07 aanair08

@carsonbot, same problem, sf 6.3

ksn135 avatar Aug 11 '23 16:08 ksn135

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

peteracombina avatar Jun 05 '24 00:06 peteracombina