如何将类添加到fieldset?

时间:2016-08-22 14:30:53

标签: zend-framework2 fieldset zend-form2

我在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);
} 
...

这意味着将类添加到字段集(或图例,如果需要)的唯一方法是扩展视图助手。

1 个答案:

答案 0 :(得分:2)

我按照发布here ( https://stackoverflow.com/a/27273068/351785)的回答。

从答案(根据我的要求修改):

  

创建Application\Form\View\Helper\FormRow.php辅助类   下面:

<?php
/**
 * Extend zend form view helper formrow to allow class to be added to fieldset / legend
 */
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRow as ZendFormRow;

class FormRow extends ZendFormRow
{

    /**
     * Utility form helper that renders a label (if it exists), an element and errors
     *
     * @param  ElementInterface $element
     * @throws \Zend\Form\Exception\DomainException
     * @return string
     */
    public function render(\Zend\Form\ElementInterface $element)
    {
        //... other code here

            // 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
            ) {
                $fieldset_class = $legend_class = '';
                if($class = $element->getOption('fieldset_class')) {
                    $fieldset_class = sprintf(' class="%s"', $class);
                }
                if($class = $element->getOption('legend_class')) {
                    $legend_class = sprintf(' class="%s"', $class);
                }
                $markup = sprintf(
                    '<fieldset%s><legend%s>%s</legend>%s</fieldset>',
                    $fieldset_class,
                    $legend_class,
                    $label,
                    $elementString);
            }
        //... other code here

        return $markup;
    }

}
     

并在Module.php的onBootstrap()方法中覆盖工厂   文件如下:

namespace Application;

use Zend\Mvc\MvcEvent;
use Zend\View\HelperPluginManager;

class Module
{
    /**
     * On bootstrap for application module.
     *
     * @param  MvcEvent $event
     * @return void
     */
    public function onBootstrap(MvcEvent $event)
    {
        $services = $event->getApplication()->getServiceManager();

        // The magic happens here
        $services->get('ViewHelperManager')->setFactory('formrow', function (HelperPluginManager $manager) {
            return new \Application\Form\View\Helper\FormRow();
        });
    }
}

并按类添加类:

$this->add(array(
    'name' => 'animals',
    'type' => 'radio',
    'attributes' => array(
        'id' => 'animals',
        'class' => 'form-control',
    ),
    'options' => array(
        'label' => 'Favourite animal',
        'fieldset_class' => 'form-group', //<== this
        'legend_class' => 'form-legend',  //<== and this
        'options' => array(
            'cat' => 'Cat',
            'dog' => 'Dog',
            'fish' => 'Fish',
        ),
    ),
));