php - Symfony2 - Get Form value, not key -
i'm new symfony2 (version 2.7) , i'm trying value submitted form. using $form->getdata()
, index corresponding selected value in list.
for example, assuming $test array:
$form = $this->createformbuilder($test) ->add('abc', 'choice', array('choices' => array( '0' => 'option1', '1' => 'option2', '2' => 'option3' ))) ->getform(); $form->handlerequest($request); if ($form->isvalid()) { $data = ($form->getdata()); }
then, $data
1
if select option2
. i've tried set choices_as_values
true, got array indexes in drop-down list.
what can $data
equal option2
?
$values = array('option1', 'option2', 'option3'); $form = $this->createformbuilder($test) ->add('abc', 'choice', array('choices' => $values)) ->getform(); $form->handlerequest($request); if ($form->isvalid()) { $data = $form['abc']->getdata(); //returns option1 if option1 selected etc.. $choice = $values[$data]; //returns choice value $choice = $data; }
Comments
Post a Comment