PM-Riabel PM-Riabel - 1 year ago 73
PHP Question

Symfony3: Why choices submit key text instead of its value

Symfony version 3.1.3

I am generating a choices list from the database like bellow and it works fine.

class ClassType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
array('class' => 'PIE10Bundle:Classes',
'expanded' => false,
'multiple' => false,));
// some other form elements

in the web page it gives HTML Select like below,

<select name="class[class]" id="class_class">
<option value="6">1A</option>
<option value="7">4C</option>

and in the controller, I am getting the submitted data and debug it as below,

if( $form->isSubmitted() && $form->isValid() )
$cName = $form['class']->getData();
echo $cName;

My result is
(depending on the selection) but not
I am expecting to get the values of the choice list but not its key text.

I wonder how this happens and how to fix this to get the value. Further I have another choice list in the same form but it submits the expected value.


Answer Source

try with


I suppose the $cName is an istance of the PIE10Bundle:Classes entity (try dumping a get_class($cName)) and the echo print the name because is implemented as a __toString method.

Hope this help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download