Оберните кучу вариантов внутри стола в Symfony / Twig

0

Я хочу сгруппировать много кучек флажков, перечисленных ниже, с тем же ярлыком:

Желаемый виджет

У меня есть этот код:

$builder->add('cleanliness', 'choice', array(
    'choices'   => array('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree'),
    'multiple'  => true,
    'expanded' => true
));
$builder->add('waitingTime', 'choice', array(
    'choices'   => array('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree'),
    'multiple'  => true,
    'expanded' => true
));
... And a bunch of other checkboxes piles
Теги:
twig

1 ответ

1
Лучший ответ

В контроллере:

$answers = array('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree');

// ...

$builder->add('cleanliness', 'choice', array(
    'label' => 'The clinic is clean',
    'multiple' => true,
    'expanded' => true,
    'choices' => $answers
));
$builder->add('waitingTime', 'choice', array(
    'label' => 'My waiting time was reasonable',
    'multiple' => true,
    'expanded' => true,
    'choices' => $answers
));

// ...

return array('form' => $form->createView(), 'answers' => $answers);

Тогда в представлении...

<table border="1">
  <tr>
    <td></td>
  {% for answer in answers %}
    <td>{{ answer }}</td>
  {% endfor %}
  </tr>
  <tr>
  <td>{{ form_label(form.waitingTime) }}</td>
  {% for choice in form.waitingTime %}
    <td>{{ form_widget(choice) }}</td>
  {% endfor %} 
  </tr>
  <tr>
  <td>{{ form_label(form.cleanliness) }}</td>
  {% for choice in form.cleanliness %}
    <td>{{ form_widget(choice) }}</td>
  {% endfor %} 
  </tr>
  {# and so on... #}
</table>

В любом случае, это общий смысл. Если эти варианты действительно являются флажками или переключателями, поэтому они могут выбирать только один из них? Если это так, установите расширенный набор равным true, а несколько - false.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню