Reputation: 498
I feel like I am missing something obvious here, I have an entity form field that gives a list of users to select from , the idea here is creating a project and associate it with that (or several) users. I achieved this without much problems but I can't figure out how to access and work with that form field.
Here's how I am setting the form field:
->add('user', 'entity', array(
'class' => 'DevUserBundle:User',
'label' => 'Assigned Users: ',
'multiple'=> true,))
In the controller I do the following:
$data = $form->getData();
I can access the field with $data['user']
but beyond that I am lost.
Upvotes: 0
Views: 3030
Reputation: 31959
To see which user has been selected, the synthax is similar to what you suggested:
$usersSelected = $form["user"]->getData();
The reason why you have such a long list in your print_r($userSelected)
statement is because $userSelected
is an array of User
objects. Indeed, as you can see in your builder: ->add('user', 'entity'...)
You can verify this this way
$i = 1;
foreach ($usersSelected as $user)
{
echo "User number ".$i;
echo get_class($user);
//Assuming that you have the method getUsername() in you User entity
echo "Username is".$user->getUsername();
$i++;
}
Upvotes: 1