yii2 ajax 验证不显示错误

yii2 ajax validation does not show errors

I have ActiveForm in a widget, and want to validate it fields(get errors under the field) without reloading the page. But the best result, i have got is the json with errors from validating action.'ValidateOn..' - seems that they arent work at all, i also try to catch 'beforeValidate' callback, and it doesnt work too. I try many variants and almost decide that it is impossible. May be somebody see my main errors, if so, help me please.


窗体视图
 <div class="feedback-form__wrap">
                        <?php $form = ActiveForm::begin([
                            'class' => 'feedback-form__form',
                            'id' => 'feedback-form',
                            'enableAjaxValidation' => true,
                            'enableClientValidation' => false,
                            'ajaxDataType' => 'json',
                            'validateOnChange' => true,
                            'validateOnType' => true,
                            'validateOnBlur' => true,
                            'validationUrl' => '/feedback/validate',
                        ]) ?>
                        <div class="custom-input-wrapper">
                            <?= Html::activeTextInput($feedbackForm, 'name', [
                                'class' => 'custom-input',
                                'required' => 'required',
                                'placeholder' => 'Имя',
                                'enableAjaxValidation' => true,
                            ]); ?>
                        </div>
                    ...   

Controller (FeedbackController)

 ` public function actionSubmit()
            {
                $feedbackForm = new FeedbackForm();
                if ($feedbackForm->load(Yii::$app->getRequest()->post())) {
                            $message = ['to' => self::FEEDBACK_TO,
                                'subject' => $feedbackForm->messageType . self::FEEDBACK_SUBJECT];
                            $mailer = Yii::$container->get(Mailer::class);
                            $viewData = [
                                'name' => $feedbackForm->name,
                                'surname' => $feedbackForm->surname,
                                'phone' => $feedbackForm->phone ?? "",
                                'email' => $feedbackForm->email,
                                'messageType' => $feedbackForm->messageType,
                                'messageText' => $feedbackForm->message
                            ];
                            $mailer->send($message, 'feedback/feedback-layout', $viewData);
        
                            $response = new Response();
                            $response->statusCode = 200;
                            $response->data = json_encode(["status" => "success"]);
                        }
                return $response ?? new Response();
            }
        
            /**
             * @return array
             */
            public function actionValidate()
            {
                $model = new FeedbackForm();
                if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
                    Yii::$app->response->format = Response::FORMAT_JSON;
                    return ActiveForm::validate($model);
                }
                return null;
            }
`
    

My Form class(rules and methods)

     `
   `

 class FeedbackForm extends Model
        {
          
            public function rules(): array
            {
                return [
                    [['name','surname','email','message'],'required'],
                    [['phone','messageType'],'string'],
                    ['email', 'validateEmail'],
                ];
            }
        
            public function validateEmail(): void
            {
                if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)){
                    $this->addError('email','Неверный формат почты');
                }
            }
        }
    `

P.S. I work with yii second day, so be condescending please :)

首先,您应该将 Html::activeTextInput 更改为 $form->field($feedbackForm, 'name')->textInput 模式

因为 $form->field 生成错误块、验证错误等等

替换

<?=
$form->field($feedbackForm, 'name')->textInput([
    'class' => 'custom-input',
    'required' => 'required',
    'placeholder' => 'Имя',
]);
?>

而不是

<?=
Html::activeTextInput($feedbackForm, 'name', [
    'class' => 'custom-input',
    'required' => 'required',
    'placeholder' => 'Имя',
    'enableAjaxValidation' => true,
]);
?>

如果您的问题仍然存在,请告诉我。