开发者

Zend_Validate_GreaterThan ignores equal values

I have the next validation:

$gvGreaterThanPvValidate = new Zend_Validate_GreaterThan(array('min' => 100));
$gvGreaterThanPvValidate->setMessage('GV should be greater than PV or equal');
$gv->addValidator($gvGreaterThanPvValidate);

According the Zend documentation it should returns TRUE for value = 100. But f开发者_如何学运维or equal value this validator return FALSE. Can you help me? Sorry for my english.


This is the code from GreaterThan validator. So it return false if the numbers are equals.

    if ($this->_min >= $value) {
        $this->_error(self::NOT_GREATER);
        return false;
    }
    return true;

And the doc says : Returns true if and only if $value is greater than min option So if the values are equals it returns false


You can use the Between validator, that have "inclusive" switch. Just set max to somethng really big like PHP_INT_MAX. It's a hack, but it works


Thought I would answer this as I was searching for the same thing and after reading the actual code I note that there is an "inclusive" option, set this to true and you have your GreaterThanOrEqualTo validator:

This is for ZF2:

        $this->add([
            'name' => 'bill_total',
            'required' => true,
            'filters'    => [
                ['name' => 'StringTrim']
            ],
            'validators' => [
                [
                    'name'    => 'greaterThan',
                    'options' => [
                        'min' => 5,
                        'inclusive' => true
                    ]
                ]
            ]
        ]
    );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜