How to validate in Zend Framework if a float/double is equal or greater/less than a min?

4.8k Views Asked by At

I've a simple question for which I've not found an answer and that is

How can I use the built-in Zend Validator to test if a float/dobule is Greater than or Equal of a min?

I've already searched for an answer that fits my request and I discovered this question GreaterOrEqual validator in Zend Framework but mine is a little bit different. Besides, I know that I can create my own Validator or copy one of those that are on the web like this one Greater Than or Equal Validator but I would like to know how I can do this validation with the built-in validators.

This is an example to let you better understand.

If I had an integer, I can achieve this goal in this way:

$Validators = array(new Zend_Validate_Int(), new Zend_Validate_GreaterThan($min - 1));
// Validate the number

Instead, If I had a float, I can't do this trick and, as a lot of programmers know, trying to do dirty things with float can create a lot of issue because of the rounding problem (What Every Computer Scientist Should Know About Floating-Point Arithmetic).

Thanks to all

3

There are 3 best solutions below

1
On BEST ANSWER

After I've tried many ways, I concluded that actually (Zend Framework version 1.11) it's no possibile to do this kind of validation using built-in validators (as suggested by Phil).

I really hope they will add this validator in the next releases.

0
On

What about using

new Zend_Validate_Callback( function ($value) { if ($value >= 1) { return true; } return false; });

0
On

It's not exactly what You asked for, but I think it's the simplest: The key is 'inlusive'.

    $number->addValidator('Float');
    $number->addValidator('Between', false,array('min' => '5,5', 'max' => $greatest_number+1, 'inclusive' => true));

here You have all the validators:

Zend_Validate_Abstract Class Reference

so the answer for Your question is 'no', there isn't Greater Than or Equal Validator for floats.