views:

24

answers:

1

Right now, I have Zend form elements wrapped in list-items. Form is validating or coughing up errors as it should. But I'd like to apply a class to the list-items of offending elements (but only to the list-items offending elements) on failed validation. Example:

This field has valid input

<li>
    <input type="text" name="address2" id="address2" value="" size="25">
</li>

but this one does not

<li class="error">
    <input type="text" name="address2" id="address2" value="" size="25">
</li>

The only way I can see to do this is to extend Zend_Form_Element, and override isValid() and then extend all classes derived from Zend_Form_Element I need for my form(s).

There has got to be a better way.

Right?

What am I missing?

THANKS

+1  A: 

I think you're better off modifying the HtmlTag decorator based on the state of the form element.

$tag = $element->getDecorator('HtmlTag');
$tag->setOption('class', 'error');

There are probably a number of different places you can invoke this when validation. You could do it in your view, or detect the presence of errors on each field when you construct the form.

You may also want to look into creating your own decorators as well.

These posts here and here do a good job of covering the overly-complex world of Zend_Form decorators.

Bryan M.
Thanks. Wound up doing it as you suggested: in the form, checking `$this->form->getElement('foobar')->getErrors()`.
Clayton