PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] Symfony - ChoiceType with multiple and resetViewTransformers returns invalid_message

 March 12, 2022     php, symfony, symfony-forms     No comments   

Issue

We have upgraded our project to Symfony 4.4.35 2 months ago and now I noticed a strange behavior. When I use ChoiceType with the following configuration, the form returns error (This value is not valid):

$builder->add('items', ChoiceType::class, [
    'required' => FALSE,
    'multiple' => TRUE,
]);
$builder->get('items')->resetViewTransformers();

The POST:

form[items][0]: "val1"
form[items][1]: "val2"

Can someone tell me what has changed? I have tried to inspect ChoiceType.php, and find out the problem is here:

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$unknownValues, $messageTemplate) {
    if (\count($unknownValues) > 0) {
        // here is append error to form
    }
});

The question is: How to create multiselect with variable length and custom values?


Solution

I had to use Symfony\Component\Form\Extension\Core\Type\CollectionType with option allow_add.

$builder->add('items', CollectionType::class, [
    'allow_add' => TRUE,
]);


Answered By - MakoBuk
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing