【发布时间】:2016-12-29 03:53:12
【问题描述】:
我在 ZF2 中有一个表单,其中添加了以下元素:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));
在我的视图脚本中,我有以下行:
<?php echo $this->formrow($form->get('animals')); ?>
生成以下html:
<fieldset>
<legend>Favourite Animal</legend>
<label><input type="radio" name="animals" id="animals" class="form-control input-error" value="cat">Cat</label>
<label><input type="radio" name="animals" class="form-control input-error" value="dog">Dog</label>
<label><input type="radio" name="animals" class="form-control input-error" value="fish">Fish</label>
</fieldset>
如何向字段集添加类?
我尝试将以下内容添加到 options 数组、attributes 数组中,并作为主数组的一个选项,但它没有将类添加到字段集:
'fieldset_attributes' => array(
'class' => 'form-group',
),
[编辑]
查看代码 (\Zend\Form\View\Helper\FormRow::render) 我发现了这个:
...
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) {
$markup = sprintf('<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString);
}
...
这意味着将类添加到字段集(或图例,如果需要)的唯一方法是扩展视图助手。
【问题讨论】:
标签: zend-framework2 fieldset zend-form2