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
It should be:
['x', 'compare', 'compareValue' => 100, 'operator' => '<'],
and
['x', 'compare', 'compareValue' => 100, 'operator' => '>='],
accordingly.
Read more in official docs.
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.
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'],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With