5

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 ;-)

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • What do you want to compare with GreaterOrEqualThan or GreaterThan? I think zend framework implementation ends at some point because it's still a framework that needs to be customized for your needs. However interesting question ;) – DarkLeafyGreen Apr 20 '11 at 12:30
  • I created my own GT and LT validators that accept a 'strict' option. – David Weinraub Apr 20 '11 at 12:41
  • @ArtWorkAD: well, some zend validators accepts additional parameters that control the validator behaviour. Such as "strict" parameter for `identical`, "allowWhiteSpace" for `alnum`, "inclusive" (!!!!!!!) for `between`, etc. So it is "inclusive" for between, but no such option for `greaterThan`. It is not fair ;-) – zerkms Apr 20 '11 at 12:51
  • @David Weinraub: Yeah, it is easy. My question is just kind of curiosity, may be some one here know what idea they followed. – zerkms Apr 20 '11 at 12:52

1 Answers1

4

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.

Till
  • 22,236
  • 4
  • 59
  • 89
  • Yes, I used `int` validator and specified `-1.1` value as `min`, since I need `>= -1`. But this looks like a dirty hack ;-) – zerkms Apr 21 '11 at 01:21
  • Yeah, I'd extend GreaterThan and make it work. Let me know if you need help. – Till Apr 21 '11 at 09:57
  • Cool, just checking. :-) Regardless, it would be nice if you added an issue on Zend's Jira and possibly a patch! :-) – Till Apr 21 '11 at 22:27