Jquery yii2ajax表单提交如何在收到json响应时获得相应输入的错误
我的模型规则得到了Json的响应,但我该如何将其映射到视图文件中的输入 好心帮忙 控制器Jquery yii2ajax表单提交如何在收到json响应时获得相应输入的错误,jquery,json,ajax,yii2,yii2-advanced-app,Jquery,Json,Ajax,Yii2,Yii2 Advanced App,我的模型规则得到了Json的响应,但我该如何将其映射到视图文件中的输入 好心帮忙 控制器 public function actionUpdateprofile(){ $model= new User; $model->scenario = 'updateprofile'; if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post(
public function actionUpdateprofile(){
$model= new User;
$model->scenario = 'updateprofile';
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
$model->validate();
// return $this->render('index',['model'=>$model]);
if($model->hasErrors()) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
} else return 'success';
}
}
查看
$('form#edit-profile-form').on('beforeSubmit', function (e) {
// send data to actionSave by ajax request.
$.post($(this).attr('action'),$(this).serialize()).done( function(response){
} ).fail();
return false; // Cancel form submitting.});
为此,您需要有
2.0.6
版本,因为这在yii2.0.6
之前是不可能的。以下是示例使用代码:
// add error for single attribute
$('#contact-form').yiiActiveForm('updateAttribute', 'contactform-subject', ["I have an error..."]);
// remove error from single attribute
$('#contact-form').yiiActiveForm('updateAttribute', 'contactform-subject', '');
// Or update the whole form and, optionally, summary at once:
$('#contact-form').yiiActiveForm('updateMessages', {
'contactform-subject': ['Really?'],
'contactform-email': ['I don\'t like it!']
}, true);
我从Yii框架发布版中获取了上述代码片段