如果验证失败,是否有任何简单的方法可以添加错误类以使用Zend framework形成字段?

#0

I was just about to ask this question myself with my own solution, to provide for the community, so to speak, when I saw you question pop up in the suggestions.

My simple and pretty elegant solution, if I say so myself, uses a simple custom decorator. It does nothing to the content it receives, but it alters the element.

class App_Form_Decorator_ErrorClass
    extends Zend_Form_Decorator_Abstract
{

    protected $_placement = null;

    protected $_options = array(
        'class' => 'error'
    );

    public function render( $content )
    {
        $element = $this->getElement();

        if( $element->hasErrors() )
        {
            $errorClass = $this->getOption( 'class' );
            $currentClass = $element->getAttrib( 'class' );
            $element->setAttrib( 'class', ( !empty( $currentClass ) ? $currentClass . ' ' . $errorClass : $errorClass ) );
        }

        return $content;
    }
}

Usage:
All you need to do is add the decorator before the ViewHelper decorator, and your set.

public function init()
{
    $elementDecorators = array(
        'ErrorClass',
        'ViewHelper',
        // etc..
    );

    // or:
    $elementDecorators = array(
        array(
            'ErrorClass',
            array( 'class' => 'custom-class' ) // defaults to 'error'
        ),
        'ViewHelper',
        // etc..
    );

    // then just add the decorators to an element the way you usually do, for instance like so:
    $someElement = new Zend_Form_Element_Text( 'someElement' );
    $someElement->setDecorators( $elementDecorators );

    // etc...

O, PS.: Be sure to add the correct prefix path in your form:

$this->addPrefixPath( 'App_Form', 'App/Form' ); // or your own namespace

#1

Yes use Dojo javascript library (although setting it up can be a little hard) Try this to get you started http://techchorus.net/add-cool-zend-dojo-date-picker-form-element-without-writing-single-line-javascript

This is how it looks : enter image description here

#2

The simplest way i found to do this was extending the isValid() method of the corresponding Form Class in use, preferrably set into a My_Standard_Form Class in your library.

public function isValid($data)
{
    $valid = parent::isValid($data);

    foreach ($this->getElements() as $element) {
        if ($element->hasErrors()) {
            $oldClass = $element->getAttrib('class');
            if (!empty($oldClass)) {
                $element->setAttrib('class', $oldClass . ' error');
            } else {
                $element->setAttrib('class', 'error');
            }
        }
    }

    return $valid;
}

Credits for the solution go to the Website Factors Blog (http://www.websitefactors.co.uk/zend-framework/2011/06/error-class-on-form-field-errors-using-zend-form/).

推荐文章

在wpf中呈现HTML字符串

在wpf中呈现HTML字符串

推荐文章

图像跨越多个列

图像跨越多个列

推荐文章

mongodb上的不区分大小写查询

mongodb上的不区分大小写查询

推荐文章

如何刷新数据网格

如何刷新数据网格

推荐文章

当src包含单词成人时删除标记

当src包含单词成人时删除标记

推荐文章

如何通过反射获取属性的DisplayAttribute?

如何通过反射获取属性的DisplayAttribute?

推荐文章

火表按键事件

火表按键事件

推荐文章

使用C++的Informix对象接口实现存储过程/函数的输出

使用C++的Informix对象接口实现存储过程/函数的输出

推荐文章

当导航到另一个页选项卡时,ASP.NET页会导致验证错误

当导航到另一个页选项卡时,ASP.NET页会导致验证错误

推荐文章

为shell脚本指定命令行参数,如name=value pairs

为shell脚本指定命令行参数,如name=value pairs

推荐文章

直接用“header()”调用发送cookie头可以吗?

直接用“header()”调用发送cookie头可以吗?

推荐文章

在Windows上创建BOM(物料清单)文件

在Windows上创建BOM(物料清单)文件

推荐文章

PHP智能数据库输出

PHP智能数据库输出

推荐文章

WPF-将全局资源耦合到ViewModel

WPF-将全局资源耦合到ViewModel

推荐文章

不带变量的php$\u GET

不带变量的php$\u GET

推荐文章

jquery ajax不工作

jquery ajax不工作