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!



September 19th, 2007 at 20:38 #
I'm not sure if this is mentioned in the official docs, but if you aren't interested in the particular type of validation error thrown by a validator in a Zend_Input_Filter chain, you don't have to assign your custom message to a constant. Rather, you can just assign a single message to the validator.
For instance, the two messages for Zend_Validate_Between are pretty similar, and you might not want to have to assign a message to both constants. In this case, the following code would allow you to specify a custom message for both errors NOT_BETWEEN and NOT_BETWEEN_STRICT:
$validators = array(
'salary' => array(
array('Between', 10000, 20000),
'messages' => array(
'Salary must be between $10,000 and $20,000')));
September 20th, 2007 at 05:08 #
Cheers Steve!
Regards,
Rob...
March 9th, 2008 at 01:38 #
Theres a bug in Zend_Input_Fitler at the moment, it kind of drove me nuts for a few hours.
Validators with stringEmpty validation messages cannot have those messages overwritten by the ones you define in options.
So even if you define a custom 'notEmptyMessage' in the options, the alnum validator will still send back its default notEmpty message.
See http://framework.zend.com/issues/browse/ZF-1912
March 9th, 2008 at 01:40 #
Oh I should have mentoned in the last post, you can still use define messages for Zend_Validate_Alnum::STRING_EMPTY in the validators array you send Zend_Filter_Input, it just means a lot of extra lines on complex forms until they fix the bug.
$validators = array( 'month' => array( 'Alnum', array('Between', array(1, 12)), 'messages' => array( array( Zend_Validate_Alnum::STRING_EMPTY => "A month value is required", Zend_Validate_Alnum::NOT_ALNUM => "Month must only consist of numbers or letters" ), 'Month must be between 1 and 12' ) ) ) );