开发者

How to add a specific class to an input which has generated a form error?

I want to add a specific class to an input if an error is genereted by the input.

For example, if input is empty and has required validator it shouls look like this:

<dd id="login-element">
    <input type="text" nam开发者_StackOverflow中文版e="login" id="login" value="" class="input-text error" />
    <ul class="errors">
        <li>Value is required and can't be empty</li>
    </ul>
</dd>

class="input-text error"

Please tell me how to do that.


I solved it with jquery! :)

I used the standard Decorators and manipulate the class of the preview Object in DOM.

$('ul.errors').prev().addClass("FieldError");


Have a look at this tutorial to get a better understanding where to change your code: http://devzone.zend.com/article/3450-Decorators-with-Zend_Form

In your case, I would recommend either replacing the ViewHelper decorator with a decorator that creates the output you want. You can extend the Zend_Form_Decorator_ViewHelper class for this with your own code and overwrite the getElementAttribs() method to insert your class attribute.


Rather than creating a Decorator, easy way to do this is set the class while you add the element in the form class. For example, you can use the following in the init() method of your Zend_Form class

$this->addElement('text', 'email', array(
    'label'      => 'Your email address:',
    'required'   => true,
    'filters'    => array('StringTrim'),
    'validators' => array(
        'EmailAddress',
    ),
    'class' => 'input-large'
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜