Zend Form:如果需要选项设置为true,如何自动将元素的类设置为required?

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!

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.