Forms Processing with the Zend Framework, Page 2
Customizing Error Messages
When an error occurs, the getMessages()
method will retrieve a list of predefined error messages.
However, these messages can be quite terse; For instance, in
the case the Zend_Validate_NotEmpty() validator
determines a value to be empty, the following error message
will be provided:
Value is required and can't be empty
This isn't exactly an ideal message, if for any other reason because it doesn't tell the user precisely which value is required and can't be empty. You can override the default messages
$validator->setMessages(array(Zend_Validate_NotEmpty::IS_EMPTY => "Please provide your name."));
Next, rather than output the error messages using a
for loop within your controller action (which
would break the MVC paradigm), create an array used to
contain errors, and then assign the
getMessages() return value to it:
$this->view->errors = array(); $this->view->errors = $validator->getMessages();
Finally, to present the errors in a well-organized fashion, I like to use a custom view helper which can be repeatedly used throughout the Web site as desired:
<?php
class Zend_View_Helper_Errors extends Zend_View_Helper_Abstract
{
public function Errors($errors)
{
echo "<div id='errors'>";
echo "<p>Errors were found:</p>";
echo "<ul>";
foreach ($errors AS $error) {
echo "<li>$error</li>";
}
echo "</ul>";
echo "</div>";
}
}
?>
Within the view, you can call the helper like this:
<?php if (count($this->errors) > 0) { ?>
<?= $this->Errors($this->errors); ?>
<?php } ?>
After some CSS styling, attempting to submit the form with a blank name will produce the error message shown in Figure 2.

Figure 2. Displaying an error message
Validating Multiple Fields
Of course, chances are your form will contain multiple
fields, such as the aforementioned registration form.
Because you'll need to validate each field, it's possible
that several errors could crop up. You'll need to keep track
of all errors and present them to the user. To do this, I
like to eliminate the need to use the
getMessages method altogether and just use my
own simple error tracking feature, like this:
$this->errors = array();
if (! Zend_Validate::is($this->_request->getPost('name'), 'NotEmpty')) {
$this->errors[] = "Please provide your name.";
}
if (! Zend_Validate::is($this->_request->getPost('email'), 'EmailAddress')) {
$this->errors[] = "Please provide a valid e-mail address.";
}
if (count($this->view->errors) > 0) {
$this->view->errors = $this->errors;
} else {
// Continue with processing
}
Where to From Here?
Processing user input is such a commonplace task within Web sites of all sizes and purposes, that there's really no excuse to not rely upon a well-organized approach such as the one offered by the Zend Framework. In fact, forms processing is so important that the framework actually offers quite a bit more functionality than what has been introduced in this tutorial. Check out the following links for more information!
- The Zend_Form Component: This component gives developers the ability to not only programmatically create forms, but also filter and validate input.
- The Zend_Filter_Input Component: This component offers developers a means for declaratively identifying which form fields must be validated.
About the Author
Jason Gilmore is founder of EasyPHPWebsites.com A>, and author of the popular book, "Easy PHP Websites with the Zend Framework". Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industrys most respected publishing programs. Over the years he's authored several other books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.
Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developers conference, and was a member of the 2008 MySQL Conference speaker selection board.
Jason has published more than 100 tutorials and articles within prominent publications such as Developer.com, Linux Magazine, and TechTarget.
