简体   繁体   中英

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 . 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.

,

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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