Yii2: Conditional Validator always returns required

前端 未结 5 1489
-上瘾入骨i
-上瘾入骨i 2021-02-07 17:24

I am trying to use Yii2\'s conditional validator as given in the guide like:

Model code

public function rules()
{
   // $discharged = function($model) {          


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 18:03

    It seems that by default Yii2 will do validation in BOTH Server side and Client side. Check the example in Conditional Validation part of Yii2 doc:

    ['state', 'required', 'when' => function ($model) {
        return $model->country == 'USA';
    }, 'whenClient' => "function (attribute, value) {
        return $('#country').val() == 'USA';
    }"],
    

    you also need a 'whenClient' code, or as @Alexandr Bordun said, to disable the client validation by 'enableClientValidation' => false.

提交回复
热议问题