T
T
tincap2015-06-01 13:22:37
JavaScript
tincap, 2015-06-01 13:22:37

How does native Ajax validation work in Yii2?

Let's say I want to check if a field is unique.

In the view I enable enableAjaxValidation:

$form = ActiveForm::begin([
    'id' => 'signup-form',
    'enableAjaxValidation' => true,
]);

In the controller action, I receive an Ajax request:
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}

Okay, that's all clear. But how to process this request now? Who is this ActiveForm::validate($model) returning to?
Naturally, I need to make sure that the data is sent without reloading the page.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
saskasa, 2016-04-17
@saskasa

To submit form data without reloading the page, hook the event:

$('#signup-form').on('beforeSubmit', function(e){
  console.log('beforeSubmit');
  return false;
});

The beforeSubmit event is called before the form is submitted when all validations have passed.

A
Access Denied, 2016-04-21
@AccessDenied80

The request will be processed without your participation, if you have done everything correctly.
Look at the lesson, everything will be clear here https://www.youtube.com/watch?v=URhvUwebSvc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question