[英]How to specify a validation rule in Yii2 which will be greater than or less than of a specific number or value?
我有一個帶有驗證規則的模型,例如:
[['x'], 'integer'],
[['x'], 'unique'],
現在我該如何添加如下規則:
x <100
或類似的東西
x> = 100
它應該是:
['x', 'compare', 'compareValue' => 100, 'operator' => '<'],
和
['x', 'compare', 'compareValue' => 100, 'operator' => '>='],
因此。
閱讀官方文檔中的更多內容。
您還可以在數字或整數驗證器上使用min
屬性:
['age', 'integer', 'min' => 0],
['amount', 'number', 'min' => 0],
還有一個max
選項。
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'],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.