Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kohana validation: correct syntax for range rule

In setting up validation for one of my models, I'm having trouble getting the correct syntax for the 'range' rule. Every variation seems to pass only the (first) min parameter and not the (second) max.

/**
 * @var   array  Validation rules
 */
public function rules()
{
    return array(
        'title' => array(
            array('not_empty'),
            array('max_length', array(':value', 50)),
        ),
        'time' => array(
            array('not_empty'),
            array('date'),
        ),
        'date' => array(
            array('not_empty'),
            array('date'),
        ),
        'limit' => array(
            array('digit'),
            array('range', array(':value', 1), array(':value', 255)),
        ),
    );
}

I also tried array('range', array(':value', array(1, 255))) to no avail.

Any suggestions?

like image 860
Rookwood Avatar asked Dec 04 '22 13:12

Rookwood


2 Answers

The correct syntax for Range Rule need 3 param and not 2. As you can see in the documentation: http://kohanaframework.org/3.2/guide/api/Valid#range

So the code must be like this:

array('range', array(':value', 1, 255)),
like image 62
gabrielem Avatar answered Dec 15 '22 03:12

gabrielem


And be carefull, the range is ]min;max[ not [min;max], so the limits are excluded.

array('range', array(':value', 1, 255)) => [2;254]

like image 43
devside Avatar answered Dec 15 '22 02:12

devside