I'm trying to add a custom validator to a field. It should take into account the value of another field. E.g. field A should be at most B+50%.
I've made a class implementing Zend_Validate_Interface
, but apparently Zend Form only sends in the value of the current field to the validator. How do I make the validator receive everything?
When you call
isValid
on aZend_Form
it will pass the all the data you passed to the methodYour custom validator will receive that whole array of raw values.
Example Validator
Example Form
Output
As you can see there was also a second argument passed to
isValid
, which is $context. And that contains the remaining values.An alternative would be to pass the second element to match against as an option to the Validator, e.g.
Setup
Will then print
int(1)
. As you can see, we fetched that value through the form element's API so anything you configured for validators and filters would be applied, e.g. it's not the raw value. And you can also set it to another value, etc.Also have a look at
Zend_Validate_Identical
to learn how ZF implements checking of other form elements: