Search code examples
modelyii2validationrules

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


Solution

  • It should be:

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

    and

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

    accordingly.

    Read more in official docs.