ZF: retrieve error messages in controller
In Zend Framework, while I'm开发者_StackOverflow中文版 in the controller is it possible to retrieve the error messages if form->isValid() returns false? I add form elements with a custom error message and I do not want to display the error message beneath the input field but on the top of the form.
Regards Andrea
Yes. You can get error messages when isValid() fails using getMessages() method:
if ($this->getRequest()->isPost()) {
if ($yourForm->isValid($_POST)) {
// success
} else {
// isValid fails
$errorMsgs = $yourForm->getMessages());
// process them, assign them to a view variable
// and display in a view.
// you can also create a view helper or a partial view script
// that would handle the display of the messages.
}
}
You may also remove 'Errors' decorator from your elements if you don't want to have errors shown below them.
精彩评论