My goal is to validate parameters passed in the URL, so I created a validate method that has a list of validators to run, like so:
$validators = array(
'number' => array(
'digits',
'presence' => 'required',
'messages' => array(
"%value%' is not a valid number.",
),
),
'country' => array(
'presence' => 'required',
'InArray' => array('haystack' => array('USA', 'CAN', 'AUS', 'JPN')),
'messages' => array(
"'%value%' is not a valid country code.",
),
),
// etc.
);
$valid = new Zend_Filter_Input(array(), $validators, $data);
return $valid->isValid()
The issue is that the 'InArray' validator does nothing. It doesn't raise any errors, it just doesn't work. I assume that I'm getting the syntax wrong.
What is the correct syntax for the 'InArray' validator?
To pass additional rules and properties to validators to be used with Zend_Filter_Input, create a concrete instance of the object and set it as your validator like this:
$validators = array(
'number' => array(
'digits',
'presence' => 'required',
'messages' => array(
"%value%' is not a valid number.",
),
),
'country' => array(
new Zend_Validate_InArray(
array('haystack' => array('USA', 'CAN', 'AUS', 'JPN'))
),
'presence' => 'required',
'messages' => array(
"'%value%' is not a valid country code.",
),
),
// etc.
);
The reason you have to do it like this is because there are no filter metacommands for setting the haystack when using the InArray validator. There are some basic metacommands that apply to many validators, but haystack is not one of them.
To specify the haystack, create a new Zend_Validate_InArray object directly with the require options and pass that validator to the array of validators given to Zend_Filter_Input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With