Kamil Mroczek
Kamil Mroczek

Reputation: 119

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" name="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.

Upvotes: 2

Views: 2126

Answers (3)

Nayan
Nayan

Reputation: 1605

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

Upvotes: 0

xaloy
xaloy

Reputation: 19

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

Upvotes: 1

chiborg
chiborg

Reputation: 28084

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.

Upvotes: 0

Related Questions