Custom Validation Not Working- Yii2-app-basic-Yii2

前端 未结 5 2349
借酒劲吻你
借酒劲吻你 2021-01-20 09:48

I Posted one question yesterday regarding custom validation for radio button Textfield Mandatory On Radio Button. I got answer. But, that was not exact answer. But, it solve

5条回答
  •  温柔的废话
    2021-01-20 10:03

    Actually, your action method is incorrect. When you call refresh(), you basically reload the page. If the model has not been updated in the database, you will see nothing special, and no errors since you reload a fresh model.

    This code will display the error as usual in your view :

       /**
         * @return \yii\web\Response
         */
        public function actionRegister()
        {
            // Register Model
            $model = new RegisterForm();
            if ($model->load(Yii::$app->request->post()))
            {
                $post = Yii::$app->request->post('RegisterForm');
                if ($model->validate())
                {
                     // whatever
                }
    
    //            return $this->refresh(); // do not refresh but...
            }
    
            // ... render the view with the current model, who's errors attribute is filled
            return $this->render('register', compact('model'));
        }
    

    NB : also, you don't have to call errors() in your view, the ActiveFormField rendering method takes care of it for you :

    field($model, 'CompanyName')->textInput()->label('Company Name') ?>
    

    is enough

提交回复
热议问题