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

Wednesday, March 9, 2022

[FIXED] Send choice Value instead choice key from Symfony form

 March 09, 2022     symfony, symfony-forms     No comments   

Issue

I need to send from Symfony form ChoiceType::class

But I don't need choices keys, I need to send choices values.

Is that is possible?

 $form->add('section', ChoiceType::class, array(
                'mapped' => false,
                'choices' => array(
                    1 => 'value1',
                    2 => 'value2'

                ),
          ));

I just want to send value1 if I chose value1,

not key 1 as default.


Solution

[Since Symfony 2.7] In any case you can play with choice value through choice_value option and a Closure function (Reference):

$form->add('section', ChoiceType::class, array(
    'choice_value' => function ($value, $key, $index) {
        return $value;
    } 
));

Useful for dynamic choices.



Answered By - yceruto
  • 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