blacktie24
blacktie24

Reputation: 5075

Zend Form: how to automatically set the class of an element to required if required option is set to true?

Is this possible? I'm trying to use jorn's jQuery Validation plugin, and I would like to avoid having to add 'class' => 'required' if i'm already setting the required => true option. Thx in advance for any input!

Upvotes: 0

Views: 247

Answers (1)

brady.vitrano
brady.vitrano

Reputation: 2256

Not possible using standard ZF classes. You can accomplish this by creating a custom decorator to replace the standard ViewHelper.

 class My_Form_Decorator_ViewHelper extends Zend_Form_Decorator_ViewHelper 
 { 
     public function render($content) 
     {
         $element = $this->getElement();
         if ($element->isRequired()) {
             $class  = $element->getAttrib('class'); // append to current attrib
             $element->setAttrib('class', $class . ' required');
         }
         return parent::render($content);
     }
 }

Of course, you also might want to add the prefix path to this decorator in your form.

Upvotes: 2

Related Questions