Does this type of mold fit your needs?
$userIDsArray = $userIDsArray = array('1' => 'name1','2' => 'name2','3' => 'name3','4' => 'name4'); $form = $this->createFormBuilder($initialData); foreach($userIDsArray as $userId) $form->add($userId, 'choice', array( 'choices' => $userIDsArray, 'multiple' => true, 'expanded' => true ) ); $form = $form->getForm();
For this array of users, checking for user 1 of the remaining three users and for user 3 only the fourth, this result will be issued.
array (size=4) 'name1' => array (size=3) 0 => int 2 1 => int 3 2 => int 4 'name2' => array (size=0) empty 'name3' => array (size=1) 0 => int 4 'name4' => array (size=0) empty
When visualizing the form, you can iterate over each element in the form, and then for each element that you can iterate over each choice and turn off the ones you want:
{% for formWidget in classForm %} {{ form_label(formWidget) }} {% for child in formWidget %} {{ form_widget(child) }}</td> {% endfor %} {% endfor %}
Of course, you can use different arrays for the strings in the form and for the selection, implementing the desired structure.
ggavrilut
source share