R
R
Roma Antonyuk2017-08-24 15:16:46
Yii
Roma Antonyuk, 2017-08-24 15:16:46

How to solve the problem with the validation of the form connected via ajax?

I have a view:

<div class="row">
    <div class="form-group">
        <button class="btn btn-primary" id="add_form">Add form</button>
    </div>
    <div class="col-md-12" id="form">

    </div>
</div>

When the button is clicked, the form is loaded via ajax:
Controller
public function actionAddForm()
    {
        $tourist = new Tourist();

        return $this->renderAjax('add-form', [
            'tourist' => $tourist
        ]);
    }

And the form itself
<?php $form = ActiveForm::begin([
    'action' => ['validate-form'],
    'validationUrl' => ['validate-form'],
    'enableAjaxValidation' => true,
    'enableClientValidation' => true
]) ?>
    <?= $form->field($tourist, 'name')->textInput() ?>
    <?= $form->field($tourist, 'surname')->textInput() ?>
    <?= Html::submitButton('sbmt', ['class' => 'btn btn-success']) ?>
<?php $form::end() ?>

Method for which ajax requests go:
public function actionValidateForm()
    {
        $tourist = new Tourist();

        if(Yii::$app->request->isAjax && $tourist->load(Yii::$app->request->post()))
        {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($tourist);
        }
    }

The problem is that ajax requests are not sent at all.
And client validation doesn't work
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-08-24
@webinar

that ajax requests are not sent at all.

why did you decide this? You are sure? Validation on ajax should be knocked. Check what is in the browser console, maybe there is a zoo of js errors?
you disabled it, that's
why it doesn't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question