2014-11-08 62 views
0

我想組以這種方式波紋管相同的標籤中所列複選框的重重疊疊:裹一堆選項中的Symfony /嫩枝表內

The desired widget

我有這樣的代碼:

$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 

回答

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。