【问题标题】:How to add class to fieldset?如何将类添加到字段集中?
【发布时间】: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


    【解决方案1】:

    我按照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',
            ),
        ),
    ));
    

    【讨论】:

    • 在 ZF3 中不需要在模块的 onBootstrap 方法中注入魔法。这可以在 view_helper 配置部分的 module.config.php 中完成。
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2019-08-30
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2018-06-11
    相关资源
    最近更新 更多