Reputation: 383
I have a table called 'Tasks', and 'User' has a Many-to-Many relation with tasks.
On the page I am showing:
<input type="checkbox" name="tasks[]" value="task.id"> TaskName
<input type="checkbox"> name="tasks[]" TaskName2
<input type="checkbox"> name="tasks[]" TaskName3
I want to know: On the server side, how can I get the array of checkboxes which were selected by the user?
If I get the collection then I can add it, using
User->addTasks(tasks)
Upvotes: 0
Views: 1363
Reputation: 10302
It doesn't look like you are using Symfony's built-in forms functionality. I'd highly recommend looking into it...
With Symfony forms, you can show entities as checkboxes by setting both of the "expanded" and "multiple" options each to true
:
// StackOverflow\BulbasaurBundle\Form\TrainerType.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class TrainerType extends AbstractType
{
...
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add(
'pokemon',
'entity',
array
(
"class" => "BulbasaurBundle:Pokemon",
"expanded" => true,
"multiple" => true,
"property" => "description"
)
);
}
...
}
When this form is rendered, it will display each of the Pokemon entities as checkboxes, using the __toString()
method as the checkbox's label.
An added bonus to using Symfony forms is that Symfony will manage which boxes are checked automagically. The form acts as the intermediary between the entity and the request/views. That is, when you make a request to change the object, the controller creates a form based on that object's current state, and presents it to the user. Conversely, when the user submits that data, the controller can bind the request information to the entity, and can then persist it.
More information on Symfony2 forms here: http://symfony.com/doc/current/book/forms.html
Upvotes: 1