0

When the data are sent to the server to be save it is important to perform check up again even if the clientside validation is done but at that time if the model is not valid the response returned is in json format(from getErrors/ActiveForm::validate($model)) containing the messages and attributes that have errors.

When receiving how can I affect it to the form on the client side; each error from the reponse to it field on the form?

Is there any function on client side(js) that I can call passing the response to it?

2
  • it's bit unclear what you are asking, what i figured out is something you looking for validation message : if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } Commented Dec 3, 2015 at 9:00
  • after return ActiveForm::validate($model); } receiving the reponse on client side how can I affect it to the form; I'll try the jazz's answer(it seems it will do what I am looking for) and give feed back if it works Commented Dec 5, 2015 at 7:40

1 Answer 1

1

Use

 $.each(data, function(key, val) {
                                        $("#"+key).after("<div class=\"help-block\">"+val+"</div>");
                                        $("#"+key).closest(".form-group").addClass("has-error");
                                    });

This will append error to corresponding field.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.