Answer the question
In order to leave comments, you need to log in
How to intercept response in ajax form Validation in Yii2?
Good afternoon,
There is such a form :
$form = ActiveForm::begin([
'id'=>$model->formName(),
'validationUrl'=>Url::to(['/orders/default/validation']),
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'validateOnBlur'=>true,
'validateOnChange'=>true,
'fieldConfig' => [
'template' => '<div class="c-order-checkout__group">{label}{input}{error} </div>',
'inputOptions'=>[
'class'=>'form-control h-form__input h-input-response'
],
'options' => [
'tag' => false,
],
]
]);
class ValidationAction extends Action
{
public function run() // $id
{
$model = new $this->modelClass;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
}
}
{"orders-user_name":["Необходимо заполнить «user-name»."],"orders-user_phone":["Необходимо заполнить «Телефон»."],"orders-user_email":["Необходимо заполнить «e-mail»."],"orders-customer_id":["Необходимо заполнить «Пользовать»."]}
$.each(data, function(key, val) {
$('#'+key).after('<div class='help-block'>'+val+'</div>');
$('#'+key).closest('.form-group').addClass('has-error');
});
Answer the question
In order to leave comments, you need to log in
There are events that activeForm throws, for example:
$('form').on('afterValidateAttribute', function(event, attr, msg) {
console.log(event);
console.log(attr);
console.log(msg);
});
['title','string','max'=>400, 'whenClient'=>function (attribute, value) {
return $('#country').val() === 'USA';
}]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question