Zend_Filter_Input / Zend_Validate Messages

The basic usage of Zend_Filter_Input is:


$filters = array('body' => array('StringTrim' 'StripTags'));
$validators = array('body' => 'Alpha');
$input = new Zend_Filter_Input($filters$validators$_POST);

if ($input->isValid()) {
    // do something with form
} else {
    // failed validation
    $messages $input->getMessages();
    // iterate though $messages to display to user
}

The problem is that the message you get out isn't always written as you would like. Consider the output if the body record is empty. A dump() of $messages gives this:

array(1) {
  ["body"] => array(1) {
    [0] => string(21) "'' is an empty string"
  }
}

The text isn't exactly what I would want to display to a user. To change it you pass in a messages parameter to the $validators array:


$filters = array('body' => array('StringTrim' 'StripTags'));
$validators = array( 'body' => array('Alpha',
  'messages'=>array(Zend_Validate_Alpha::STRING_EMPTY=>'The body is empty!')));
$input = new Zend_Filter_Input($filters$validators$_POST);

This works as expected. The problem now is that you need to know the "magic constants", so here is a list of all the constants and the associated default message:

Zend_Validate_Alnum::NOT_ALNUM
'%value%' has not only alphabetic and digit characters
Zend_Validate_Alnum::STRING_EMPTY
'%value%' is an empty string

Zend_Validate_Alpha::NOT_ALPHA
'%value%' has not only alphabetic characters

Zend_Validate_Alpha::STRING_EMPTY
'%value%' is an empty string

Zend_Validate_Between::NOT_BETWEEN
'%value%' is not between '%min%' and '%max%', inclusively

Zend_Validate_Between::NOT_BETWEEN_STRICT
'%value%' is not strictly between '%min%' and '%max%'

Zend_Validate_Ccnum::LENGTH
'%value%' must contain between 13 and 19 digits

Zend_Validate_Ccnum::CHECKSUM
Luhn algorithm (mod-10 checksum) failed on '%value%'

Zend_Validate_Date::NOT_YYYY_MM_DD
'%value%' is not of the format YYYY-MM-DD

Zend_Validate_Date::INVALID
'%value%' does not appear to be a valid date

Zend_Validate_Digits::NOT_DIGITS
'%value%' contains not only digit characters

Zend_Validate_Digits::STRING_EMPTY
'%value%' is an empty string

Zend_Validate_EmailAddress::INVALID
'%value%' is not a valid email address in the basic format local-part@hostname

Zend_Validate_EmailAddress::INVALID_HOSTNAME
'%hostname%' is not a valid hostname for email address '%value%'

Zend_Validate_EmailAddress::INVALID_MX_RECORD
'%hostname%' does not appear to have a valid MX record for the email address '%value%'

Zend_Validate_EmailAddress::DOT_ATOM
'%localPart%' not matched against dot-atom format

Zend_Validate_EmailAddress::QUOTED_STRING
'%localPart%' not matched against quoted-string format

Zend_Validate_EmailAddress::INVALID_LOCAL_PART
'%localPart%' is not a valid local part for email address '%value%'

Zend_Validate_Float::NOT_FLOAT
'%value%' does not appear to be a float

Zend_Validate_GreaterThan::NOT_GREATER
'%value%' is not greater than '%min%'

Zend_Validate_Hex::NOT_HEX
'%value%' has not only hexadecimal digit characters

Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
'%value%' appears to be an IP address, but IP addresses are not allowed

Zend_Validate_Hostname::UNKNOWN_TLD
'%value%' appears to be a DNS hostname but cannot match TLD against known list

Zend_Validate_Hostname::INVALID_DASH
'%value%' appears to be a DNS hostname but contains a dash (-) in an invalid position

Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'

Zend_Validate_Hostname::UNDECIPHERABLE_TLD
'%value%' appears to be a DNS hostname but cannot extract TLD part

Zend_Validate_Hostname::INVALID_HOSTNAME
'%value%' does not match the expected structure for a DNS hostname

Zend_Validate_Hostname::INVALID_LOCAL_NAME
'%value%' does not appear to be a valid local network name

Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
'%value%' appears to be a local network name but but local network names are not allowed

Zend_Validate_InArray::NOT_IN_ARRAY
'%value%' was not found in the haystack

Zend_Validate_Int::NOT_INT
'%value%' does not appear to be an integer

Zend_Validate_Ip::NOT_IP_ADDRESS
'%value%' does not appear to be a valid IP address

Zend_Validate_LessThan::NOT_LESS
'%value%' is not less than '%max%'
Zend_Validate_NotEmpty::IS_EMPTY
Value is empty, but a non-empty value is required
Zend_Validate_Regex::NOT_MATCH
'%value%' does not match against pattern '%pattern%'
Zend_Validate_StringLength::TOO_SHORT
'%value%' is less than %min% characters long
Zend_Validate_StringLength::TOO_LONG
'%value%' is greater than %max% characters long

Finally, you can also change the generic "not empty" and "missing" messages in Zend_Filter_Input using:


$options = array(
    'notEmptyMessage' => "A non-empty value is required for field '%field%'",
    'missingMessage' => "Field '%field%' is required"
);
$filters = array('body' => array('StringTrim' 'StripTags'));
$validators = array('body' => 'Alpha');
$input = new Zend_Filter_Input($filters$validators$_POST);
$input->setOptions($options);

Now you can fix those messages to say something useful and your users will thank you for it!

If you would like to comment on this article, please ping me on twitter.
If your response won't fit into 140 characters, write a blog post and then ping me!