Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are a couple of thoughts...</p> <ol> <li><p>Instead of calling isValid on every form element, you can validate your whole form with isValid, and then loop over your elements as you are in your question, but instead use <code>if ($element-&gt;hasErrors())</code> to determine if you need to add the error class to the element.</p></li> <li><p>You may want to extend from Zend_Form and add a helper method to your new Form class that does this for you. For example, add a method called <code>highlightErrorElements()</code> or something like that and if you have an unsuccessful call to <code>$form-&gt;isValid()</code>, you can then simply call <code>$form-&gt;highlightErrorElements()</code> which will loop over each form element and see if it has errors and apply the style if necessary.</p></li> </ol> <p>Example:</p> <pre><code>&lt;?php class Application_Form_Base extends Zend_Form { public function __construct() { // this is where i normally set up my decorators for the form and elements // additionally you can register prefix paths for custom validators, decorators, and elements parent::__construct(); // parent::__construct must be called last because it calls $form-&gt;init() // and anything after it is not executed } public function highlightErrorElements() { foreach($this-&gt;getElements() as $element) { if($element-&gt;hasErrors()) { $element-&gt;setAttrib('class', 'error'); } } } } </code></pre> <p>Somewhere, in a controller...</p> <pre><code>$form = new Application_Form_Register(); // this extends Application_Form_Base if ($form-&gt;isValid($this-&gt;getRequest()-&gt;getPost())) { // add user etc } else { $form-&gt;highlightErrorElements(); // other error logic } $this-&gt;view-&gt;form = $form; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload