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

前端 未结 5 2309
借酒劲吻你
借酒劲吻你 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:10

    You should to add 'enableAjaxValidation' => true in ActiveForm.

    After do this your code should be,

    controller

    public function actionRegister()
    {
        $model = new RegisterForm();
        if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
          \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
          return \yii\widgets\ActiveForm::validate($model);
        }
    
        if ($model->load(Yii::$app->request->post())) 
        {
        }
        else
        {
            // HERE YOU CAN PRINT THE ERRORS OF MODEL
            echo "
    ";
            print_r($model->getErrors());
            echo "
    "; } return $this->refresh(); }

    View

     'register-form',
               'enableAjaxValidation' => true,
    ]); ?>
        .
        .
        .
        field($model, 'AdminType')
                ->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company'))
                ->label('Are You')?>
        field($model, 'CompanyName')->textInput()->label('Company Name')->error() ?>
        .
        .
    
    

    It may help you.

提交回复
热议问题