phpyii2yii2-validation

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.


Form view
 <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 :)

Solution

  • First of all you should change Html::activeTextInput to $form->field($feedbackForm, 'name')->textInput pattern

    because of $form->field generate error block, validation errors and many more

    Replace

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

    instead of

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

    tell me if your problem still exist.