【问题标题】:Add class attribute to Form Errors将类属性添加到表单错误
【发布时间】:2012-11-25 18:48:04
【问题描述】:

我正在使用 Zend Framework 2 开发一个应用程序,我使用 FormRow 帮助器在表单中呈现标签、输入和错误(如果存在)。

//within the view
echo $this->formRow($form->get('Name'));

当用户在未填写所需输入文本字段的情况下提交表单时,FormRow 会呈现以下错误消息:

<label>
    <span>Name: </span>
    <input class="input-error" type="text" value="" placeholder="Insert Name Here" name="Name">
</label>
<ul>
    <li>Value is required and can't be empty</li>
</ul>

我怎样才能为 li 标签设置一个类来设置它的样式?

我知道我可以通过...回显带有所需类属性的 formElementErrors。

<?php echo $this->formElementErrors($form->get("Name"), array('class' => "valuerequired", 'message' => "errortestmessage")); ?>

..但是 FormRow 仍然会在没有类的情况下呈现错误消息。

仅供参考,我以这种方式设置实体:

public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();

            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name'     => 'Name',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'      => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                ),
           )));

            $this->inputFilter = $inputFilter;
        }
        return $this->inputFilter;
    }

【问题讨论】:

    标签: php css zend-form zend-framework2


    【解决方案1】:

    the code of formElementErrors

    基本上你可以这样做:

    $this->formElementErrors($elem)
         ->setMessageOpenFormat('<ul%s><li class="some-class">')
         ->setMessageSeparatorString('</li><li class="some-class">');
    

    但那很不方便......

    更好的解决方案是通过您自己的类扩展 Zend\Form\View\Helper\FormElementErrors,然后将视图助手 formElementErrors 注册到您的类。所以基本上你会有这样的东西:

    namespace Mymodule\Form\View\Helper;
    
    use Zend\Form\View\Helper\FormElementErrors as OriginalFormElementErrors;
    
    class FormElementErrors extends OriginalFormElementErrors  
    {
        protected $messageCloseString     = '</li></ul>';
        protected $messageOpenFormat      = '<ul%s><li class="some-class">';
        protected $messageSeparatorString = '</li><li class="some-class">';
    }
    

    最后一件事就是用这个新类注册视图助手。为此,您在 Modules Module.php 中提供以下代码

    public function getViewHelperConfig()
    {
        return array(
            'invokables' => array(
                'formelementerrors' => 'Mymodule\Form\View\Helper\FormElementErrors'
            ),
        );
    }
    

    displaimer:此代码未经测试,如果有错误请告诉我,但我认为这应该很好。

    【讨论】:

    • 这是一个旧线程,但只是为了增加我的两分钱:另一种注册方法是在你的 module.config.php 文件中添加一个条目到 view_helpers =&gt; invokables 而不是实际的模块.php。同样的效果,不同的方法。
    【解决方案2】:

    好的,我自己的问题的解决方案就在我面前,而不是使用:

    //within the view
    echo $this->formRow($form->get('Name'));
    

    我分别调用了表单的每个元素:

        //within the view
        echo $this->formLabel($form->get('Name'));
        echo $this->formInput($form->get('Name'));
        echo $this->formElementErrors($form->get("Name"), array('class' => "some_class", 'message' => "errormessage")); 
    

    不知道这是否是最有效的方法,如果您不这么认为,请留言。

    【讨论】:

      【解决方案3】:

      FormRow 检查“form_element_errors”插件是否注册。如果是这样,则默认使用它来显示错误消息。

      所以 Sam 的示例工作。您应该重新定义标准插件并告知 mvc。

      我已经在 module.config.php 中完成了

      'view_helpers' => array(
      'invokables' => array(
          'formElementErrors'=> 'MyModule\View\Helper\FormElementErrors',
      

      FormRow 如我所愿开始显示错误:)

      【讨论】:

        【解决方案4】:

        来自 ZF2 的文档。这是链接:http://framework.zend.com/manual/2.0/en/modules/zend.form.view.helpers.html#formelementerrors

        echo $this->formElementErrors($element, array('class' => 'help-inline'));
        // <ul class="help-inline"><li>Value is required and can&#039;t be empty</li></ul>
        

        【讨论】:

          【解决方案5】:

          您的问题,请尝试

          改变

          //within the view
          echo $this->formRow($form->get('Name'));
          

          //within the view
          echo $this->formRow($form->get('Name'),null,false);
          // Note: add more 2 last parameters, false- for $renderErrors => will NOT render Errors Message. 
          //Look original function in helper/formrow.php: function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $partial = null)
          

          将错误消息呈现为您的功能

          echo $this->formElementErrors($form->get('name'), array('class' => 'your-class-here'));
          

          【讨论】:

            【解决方案6】:

            我使用echo $this-&gt;formElementErrors($form, array('class' =&gt; "error-messages")); 在一个地方显示所有错误消息:

            echo $this->formElementErrors($form, array('class' => "error-messages"));// Print all error messagess
            
            echo $this->formLabel($form->get('Name'));
            echo $this->formInput($form->get('Name'));
            
            echo $this->formLabel($form->get('Name2'));
            echo $this->formInput($form->get('Name2'));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-09-26
              • 2014-05-07
              • 2014-11-01
              • 2018-03-16
              • 1970-01-01
              • 2015-08-30
              • 1970-01-01
              • 2023-03-11
              相关资源
              最近更新 更多