Franziska
Franziska

Reputation: 115

symfony 2.2 Forms: Numeric Value in Option FieldType

I would like to implement a select box in which the user can choose a support team he wants to contact. Every team has his own number and these numbers are not consecutive numbers.

So I add this array to the choice box:

array('1' => 'Team A', '123' => 'Team B')

But the select box says now:

<select [...]>
<option value="0">Team A</option>
<option value="1">Team B</option>
</select>

{{ dump(options) }} in {% block choice_widget_options %} shows me this:

array(2) {   
    [0] =>   class Symfony\Component\Form\Extension\Core\View\ChoiceView#1538 (3) {     public $data =>     int(0)     public $value =>     string(1) "0"     public $label =>     string(17) "Team A"   }
    [1] =>   class Symfony\Component\Form\Extension\Core\View\ChoiceView#1539 (3) {     public $data =>     int(1)     public $value =>     string(1) "1"     public $label =>     string(19) "Team B"   

}   

Does anybody know how I can fix this?

Upvotes: 0

Views: 83

Answers (2)

Franziska
Franziska

Reputation: 115

Ok guys I found the problem :)

I use "array_shift($choices);" later to delete the first team (default value) from the array. This changes the keys to 0,1,2,3,4 and so on.

Upvotes: 0

john Smith
john Smith

Reputation: 17906

well you added the keys as strings

array('1' => 'Team A', '123' => 'Team B')

change to

array(1 => 'Team A', 123 => 'Team B')

Upvotes: 1

Related Questions