I am using a Zend_Form containing Zend_Form_Subform instances, and
submitting via jQuery/Ajax, using the AjaxContext helper on the controller
side. When validation fails, I am sending a JSON data structure with the
validation errors and using a crude jQuery plugin of my own devise that
displays the errors where they belong. Works fine for simple (subformless)
forms, but I discovered that where there are subforms, $form->getMessages()
(naturally enough) groups the error messages under the subform name, while
my jQuery plugin is expecting a flat array of validation error messages. I
could make my plugin smarter, I guess, but it seems simpler to do the work
server side. So, I am ensuring unique array keys by prepending the form name
to each field name:

                $messages = $form->getMessages();
                $validationErrors = array();
                foreach (array_keys($messages) as $name) {
                    foreach($messages[$name] as $k => $v) {
                        $validationErrors["{$name}_{$k}"] = $v;
                    }
                }
                $this->view->assign(array('validationErrors' =>
$validationErrors));

Seems to work fine. Any comments so far?

But! This is going to happen in various places around the application, so I
am considering the best approach. A controller action helper that mangles
$form->getMessages() for me? Or have all my complex forms extend a base
class that overrides getMessages()? Or... ?

Thanks.

-- 
Support real health care reform:
http://phimg.org/

--
David Mintz
http://davidmintz.org/

Reply via email to