Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery yii2ajax表单提交如何在收到json响应时获得相应输入的错误_Jquery_Json_Ajax_Yii2_Yii2 Advanced App - Fatal编程技术网

Jquery yii2ajax表单提交如何在收到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(

我的模型规则得到了Json的响应,但我该如何将其映射到视图文件中的输入

好心帮忙

控制器

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框架发布版中获取了上述代码片段