Answer the question
In order to leave comments, you need to log in
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();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
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);
}
}
public function actionValidateTechnic()
{
return false;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question