Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony form design

I have an Event entity, where entities of People can attend. After an event, the host of the event sits down and is supposed to be presented with a form like this.

         user a    user b     user c      user d     user e    user f     user g
user a     _
user b               _
user c                          _
user d                                      _
user e                                                 _
user f                                                           _
user g                                                                      _

All blanks are checkboxes whether a user likes another user. Underscores are disabled checkboxes, since a user cannot like themselves. Should I use a choice_list? I want to process the input like this:

foreach(guests as guest)//horizontal
{
    foreach(guests as other)//vertical
    {
         if(guest != other && guest.likes(other) && other.likes(guest))
         {
             //do something
         }
    }
}

How would I use the formbuilder to achieve something like this?

like image 787
No_name Avatar asked Mar 13 '14 21:03

No_name


1 Answers

Doesn't this type of form suits 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 all other three users and for user 3 only the forth, will produce a result like this.

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 you render the form you can iterate over every item in form and then for every item you can iterate over every choice and disable 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 differents arrays for rows in form and for choices, implementing the structure you desire.

like image 87
ggavrilut Avatar answered Oct 05 '22 07:10

ggavrilut