问题
Realized few minutes ago that there is no GreaterOrEqualThan validator, or a parameter in GreaterThan validator that changes its behaviour from > to >=.
Why? Is it possible to compose >= validator using basic zend framework set of validators?
Yes, guys, I know that I can write my own validator, but I'm curious about solution based on native ZF validators ;-)
回答1:
I'd set array('min' => ($value-1)) and use GreaterThan. Maybe use a chain and add Digits, so you make sure you're dealing with numbers. Something like this:
$value = 10;
$chain = new Zend_Validate();
$chain->addValidator(new Zend_Validate_Digits());
$chain->addValidator(new Zend_Validate_GreaterThan(array('min' => ($value-1))));
var_dump($chain->isValid($value), $chain->getMessages());
I think that's as far as you get with ZF. Wouldn't hurt to get a feature request though. Would be a nice addition. Otherwise, extend GreaterThan and add an option.
来源:https://stackoverflow.com/questions/5730075/greaterorequal-validator-in-zend-framework