The method isValid() populates the form with the values passed to it. As you can see in the code I call this before making a decision about where to send the user. I have my forms action point to the action method that is making this decision, so when the user fails the isValid() method I don't actually forward them anywhere, rather I just don't do the DB save, and I do create an error message that is displayed. A fuller, cleaner version of this code would be:
Code:
public function indexAction()
{
// get the needed namespaces
$viewNamespace = new Zend_Session_Namespace('view');
// get the form and save to a view var
$this->view->form = $this->getEmailSenderForm();
// check if the form was submitted via post
if ($this->_request->isPost()) {
if ($this->view->form->isValid($this->_request->getPost())) {
// get the sanitized values from the form object
$sanitizedValues = $this->view->form->getValues();
// the form is valid, save to db via a method within this controller
$this->saveToDb($sanitizedValues);
} else {
// the form is not valid, send the user back to the form with a message generated by Zend_Form
$viewNamespace->arrayErrors = $form->getMessages();
}
}
}
The namespaces section that you ask about is just something I typically do in action controllers. I instantiate an instance of Zend_Session_Namespace for use in holding info that needs to be stored in the session. In this case you can see I stored the error messages generated by Zend_Form. In my header view script I have code that checks $viewNamespace->arrayErrors for any values. If there are values then it displays them in a div at the top of the page.