简体   繁体   English

基于最小值的Yii2条件验证规则

[英]Yii2 conditional validation rule on basis of minimum value

I have 2 fields in a form pricemark and price, when user select pricemark value = other user can not enter a value less than 250. I need a validation rule for it in yii2 but its not working . 当用户选择价格标记值= other用户不能输入小于250的值时,我以价格标记和价格的形式有2个字段。我需要在yii2对其进行验证的规则,但该规则无效。 here is my code 这是我的代码

        ['price', 'min' => 250, 'when' => function ($model) {
            return $model->priceMark == 'other';
        }],

Silly me, I did not specify the type of attribute for validation. 愚蠢的我,我没有指定验证的属性类型。 the correct code should be 正确的代码应该是

        ['price', 'integer', 'min' => 250, 'when' => function ($model) {
            return $model->priceMark == 'other';
        }, 'whenClient' => "function (attribute, value) {
            return $('#uploadform-pricemark').val() == 'other';
        }"],

see integer after price attribute I missed it , also I added client side code which is also working fine now. 在价格属性后看到integer ,我错过了它,还添加了客户端代码,现在也可以正常工作。

,

Try this: 尝试这个:

['price', 'number', 'min' => 250, 'when' => function ($model) {
    return $model->priceMark == 'other';
}, 'whenClient' => 'function (attribute, value) {
    return $("<field>").val() == "other";
}'],

where <field> is the priceMark element identifier like its class or ID (ie #price_pricemark if model name is Price ). 其中, <field>priceMark元素标识符,例如其类或ID(即,如果模型名称为Price #price_pricemark )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM