8

我有一个带有验证规则的模型,例如:

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

现在如何添加如下规则:

x < 100
或类似
x >= 100

4

3 回答 3

12

它应该是:

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

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

因此。

阅读更多官方文档

于 2015-03-31T06:44:29.520 回答
3

您还可以min在数字或整数验证器上使用该属性:

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

还有一个max选项。

于 2018-08-04T05:57:44.437 回答
1

Yii2 大于验证:

field_to 必须大于“field_from”。

字段 1:field_from

字段 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'],
于 2019-02-15T10:02:20.773 回答