Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii Validation rule for decimal range

I want create a rule for decimal range value 40.000<= score_percentage <=100.000

array('entrance_score', 'compare','operator'=>'<=','compareValue'=>100, 'message'=>'Maximum Entrance Score should be 100.' ),

While testing from GUI, it make acceptable the decimal number less than 100 but cannot make acceptable more or equal to 40.000.

The following rule is not working properly, so what should I do?

array('entrance_score', 'compare','operator'=>'>=','compareValue'=>0 , 'message'=>'Minimum Entrance Score should be 40.' ),
like image 969
Sujit Libi Avatar asked Oct 31 '22 20:10

Sujit Libi


1 Answers

So, you want to place a check so that entrance_score value is between 40.000 and 100.000, right ?

You could just place a min, max rule in your model, something like:

array('entrance_score', 'numerical', 'integerOnly'=>false, 'min'=>40, 'max'=>100),

or, you could create your own custom validation rule, like:

public function rules()
    {
        return array(
            //............
            array('entrance_score', 'numerical', 'integerOnly'=>false),
            array('entrance_score', 'authenticate'),
            //.....
        );
    }

and your authenticate function:

public function authenticate($attribute,$params)
    {
        if($this->entrance_score < 40) {
            $this->addError('entrance_score','Minimum Entrance Score should be 40.');
        } elseif($this->entrance_score > 100) {
            $this->addError('entrance_score','Maximum Entrance Score should be 100.');
        }
    }

that should do it.

like image 186
Criesto Avatar answered Nov 15 '22 06:11

Criesto