sonam
sonam

Reputation: 3760

checkboxes in symfony 2 forms

I have a form defined in a controller as below:

$addForm = $this->createFormBuilder()
            ->add('userIds', 'collection', array(
                'type' => 'checkbox',
                'allow_add' => true,
                'options' => array(
                    'required' => false
                )
            ))
            ->add('userId', 'hidden')
            ->getForm();

In view I am showing a datagrid with bulk delete option. I am using knp paginator for pagination. I am manually rendering fields in the view as below:

//Inside loop
 {%for items in pagination %}
<input type="checkbox" name="form[userIds][]" class="ids" value="{{items.id}}"/>
 {%endfor%}

I am fetching data in the controller after post request as below:

 var_dump($data['userIds']); 

When the user selects suppose three checkboxes , I get output as below:

array (size=4)
0 => boolean true
1 => boolean true
2 => boolean true
3 => boolean true

The values should contain userIds like 1,5,6,7 but I am getting boolean values only. What have i done wrong ?

Upvotes: 4

Views: 4488

Answers (1)

moonwave99
moonwave99

Reputation: 22817

Consider using an Entity field type: tweak with expanded and multiple properties, you will get a list of checkboxes that will be bound to your model schema upon submission.

Upvotes: 1

Related Questions