Alex Pliutau
Alex Pliutau

Reputation: 21947

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 for equal value this validator return FALSE. Can you help me? Sorry for my english.

Upvotes: 0

Views: 1775

Answers (3)

HappyCoder
HappyCoder

Reputation: 6155

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

Upvotes: 0

Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

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

Upvotes: 2

yokoloko
yokoloko

Reputation: 2860

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

Upvotes: 2

Related Questions