D
D
dikium2016-09-22 14:02:27
Yii
dikium, 2016-09-22 14:02:27

What should validate ajax request return in ActiveForm Yii2?

Hello! I am facing the following issue in Yii2:
I want to validate form ActiveForm data using ajax. To do this, I declared the form like this:

<?php
if($new_technic)
  $action = Url::to(['admin/create-technic']);
else
  $action = Url::to(['admin/update-technic', "id" => $_GET['id']]);

$form = ActiveForm::begin([
  'id' => 'technic-form',
  'action' => $action,
  'enableClientValidation' => false,
  'enableAjaxValidation' => true,
  'validationUrl' => Url::to(['admin/validate-technic']),
  'validateOnSubmit' => true,
]);
?>
<?= $form->field($technic, 'model')->dropDownList($tech_models, ['id' => "tech_model"]); ?>

<div id="model_input" style="display: none; padding-left: 48px;">
  <?= $form->field($tech_model, 'firm')->textInput(); ?>
  <?= $form->field($tech_model, 'model')->textInput(); ?>
  <?= $form->field($tech_model, 'type')->dropDownList($tech_types); ?>
</div>

<?= $form->field($technic, 'logic_id')->dropDownList($logics); ?>

<?= $form->field($technic, 'physic_id')->dropDownList($physics); ?>

<div class="form-group">
  <?php
  if($new_technic)
    $label = 'Добавить';
  else
    $label = 'Изменить';

   echo Html::submitButton($label, ['class' => 'btn btn-primary', 'name' => 'contact-button']);
  ?>
</div>

<?php
ActiveForm::end();

In this case, on validation, the form sends a request to admin/validate-technic and, if successful, goes to admin/create-technic.
The question is: what should admin/validate-technic return if the validation fails? The official docs contain the following construction:
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}

Those. an associative array is returned, in which the names of the keys are the names of the fields, and the values ​​are the error messages. Then I try to do like this:
public function actionValidateTechnic()
{
   if(Yii::$app->request->isAjax)
   {
  Yii::$app->response->format = Response::FORMAT_JSON;

  $tech = new Technic();
  $tech->load($_POST);
  return ActiveForm::validate($tech);
   }
}

And this action always validates successfully, even if ActiveForm::validate doesn't validate properly and returns errors. However, if you do it like this:
public function actionValidateTechnic()
{
    return false;
}

That validation is not carried out and the form is not submitted. But it's worth returning ActiveForm::validate($tech); how validation is carried out even if the form is filled out incorrectly. Those. ajax request returns an error message, but the form still jumps to its action.
The question is: what should the ajax request return so that the form is not validated and the necessary error messages are displayed? And what should it return on successful validation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dikium, 2016-09-23
@dikium

Oh, I rebooted my computer, started the server, and all of a sudden it worked! I don’t understand what the problem was ... Thank you all!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question