将类属性添加到表单错误

时间:2012-12-07 15:04:12

标签: php css zend-form zend-framework2

我正在使用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;
    }

6 个答案:

答案 0 :(得分:22)

请参阅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,然后将view-helper 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 :此代码未经过测试,请告知我是否存在错误,但我认为这应该可以很好地解决。

答案 1 :(得分:11)

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

//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")); 

不知道这是否是最有效的方式,如果您不这么认为,请删除一行。

答案 2 :(得分:2)

FormRow检查“form_element_errors”插件是否已注册。如果是这样,请将其用作默认显示错误消息。

Sam的例子就是这样。您应该重新定义标准插件并告知mvc。

我在module.config.php

中完成了
'view_helpers' => array(
'invokables' => array(
    'formElementErrors'=> 'MyModule\View\Helper\FormElementErrors',

和FormRow按我的意愿开始显示错误:)

答案 3 :(得分:1)

作为您的问题,请尝试

更改

//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'));

答案 4 :(得分:0)

来自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 :(得分:0)

我使用echo $this->formElementErrors($form, array('class' => "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'));