Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a validation rule in Yii2 which will be greater than or less than of a specific number or value?

I have a model with a validation rule like:

[['x'], 'integer'],
[['x'], 'unique'],

Now how can I add a rule like:

x < 100
or something like
x >= 100

like image 729
Anisur Rahaman Sakib Avatar asked Mar 31 '15 06:03

Anisur Rahaman Sakib


3 Answers

It should be:

['x', 'compare', 'compareValue' => 100, 'operator' => '<'],

and

['x', 'compare', 'compareValue' => 100, 'operator' => '>='],

accordingly.

Read more in official docs.

like image 158
arogachev Avatar answered Oct 07 '22 03:10

arogachev


You could also use the min attribute on number, or integer validators:

['age', 'integer', 'min' => 0],
['amount', 'number', 'min' => 0],

There is also a max option.

like image 35
bpanatta Avatar answered Oct 07 '22 02:10

bpanatta


Yii2 greater than validation :

field_to must be greater than "field_from".

Field 1 : field_from

Field 2 : field_to

[['field_to'], 'compare', 'when' => function($model) {
                        return $model->builtup_area != null;
                    }, 'whenClient' => "function (attribute, value){
                    return $('#form-field_from').val() != '';
                }", 'compareAttribute' => 'field_from', 'operator' => '>', 'type' => 'number'],
like image 1
Mahesh Kathiriya Avatar answered Oct 07 '22 02:10

Mahesh Kathiriya