Zend framework2 zendfamework 2表单验证设置输入的属性

Zend framework2 zendfamework 2表单验证设置输入的属性,zend-framework2,Zend Framework2,是否可以在使用输入过滤器验证表单时,不仅发送错误消息,而且将边框颜色更改设置为红色 大概是这样的: $this->add(array( 'name' => 'test', 'required' => true, 'attributes' => array( 'style' => 'border-color:red' ), 'validators' =>

是否可以在使用输入过滤器验证表单时,不仅发送错误消息,而且将边框颜色更改设置为红色

大概是这样的:

$this->add(array(
        'name' => 'test',
        'required' => true,
        'attributes' => array(
                'style' => 'border-color:red'
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'options' => array(
                    'messages' => array(
                        \Zend\Validator\NotEmpty::IS_EMPTY => 'Please fill me out'
                    )
                )
            )
        )
    ));

一种方法是创建自己的视图帮助器来呈现表单元素,如果字段有错误,则在其中添加错误类

首先,您需要创建视图帮助器,如果表单元素有错误,则添加一个类

视图辅助对象:
namespace Application\View\Helper;
use Zend\Form\View\Helper\FormInput as ZendFormInput;

use Zend\Form\ElementInterface;
use Zend\Form\Exception;

class FormInput extends ZendFormInput
{


    /**
     * Render a form <input> element from the provided $element    
     *
     * @param  ElementInterface $element
     * @throws Exception\DomainException
     * @return string
     */
    public function render(ElementInterface $element)
    {
        $name = $element->getName();
        if ($name === null || $name === '') {
            throw new Exception\DomainException(sprintf(
                '%s requires that the element has an assigned name; none discovered',
                __METHOD__
            ));
        }

        $attributes          = $element->getAttributes();
        $attributes['name']  = $name;
        $attributes['type']  = $this->getType($element);
        $attributes['value'] = $element->getValue();

        return sprintf(
            '<input %s class="form-control '.(count($element->getMessages()) > 0 ? 'error-class' : '').'" %s',
            $this->createAttributesString($attributes),
            $this->getInlineClosingBracket()
        );
    }

}

请看这里->谢谢,这很好,但不幸的是,我仍然无法将类添加到输入字段:签出此文档:。还有这个例子:,
    'view_helpers' => array(
        'invokables'=> array(
            'formInput' => 'Application\View\Helper\FormInput',
        ),
    ),