如何手动进行正确的yii2验证?

时间:2016-07-06 14:51:18

标签: javascript jquery yii2

应该在用户列表的页面上,打开模态窗口进行编辑。此页面不应更新。在AJAX填充数据后,单击该链接将打开一个模式窗口ActiveForm。我如何转移现在的id就像通过验证一样。有了帮助yii.ActiveForms.js还是别的什么?

  public function actionEditUser($id) {

if (!Yii::$app->user->isGuest && Yii::$app->user->identity->role == 2) {
  $model = new EditUserForm();
  $model->id = (int) $id;
  if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
  }
  if ($model->load(Yii::$app->request->post()) && $model->updateUser()) {
    return Yii::$app->response->redirect(Url::to(Yii::$app->request->referrer, true));
  } else {
    $user = $this->findModel($id);
    $model->login = $user->login;
    $model->email = $user->email;
    $model->fullName = $user->full_name;
    return $this->renderAjax('editModalUserForm', [
        'model' => $model,
    ]);
  }
}
return Yii::$app->response->redirect(Url::to('site/sign-in', true));

}

  InitEditUser: function (event) {
    var target = $(event.target);
    var id = target.attr('id');
    var url = 'http://' + window.location.hostname + '/users/' + id;
    var urlGet = window.location.href;
    $.ajax({
      type: 'GET',
      url: urlGet,
      data: 'id=' + id,
      success: function (data, textStatus, jqXHR) {
        console.log('ok');

      },
      error: function (jqXHR, textStatus, errorThrown) {


      }
    });




    $('#editUser').modal('show');


    $.ajax({
      type: 'GET',
      url: url,
      success: function (data, textStatus, jqXHR) {
        console.log(data);
        $('#editUserInput').val(data.login);
        $('#editUserFullNameInput').val(data.full_name);
        $('#editUserEmailInput').val(data.email);

        $('#editUserForm').on('beforeSubmit', function () {
          console.log("a");
          return false;
        });
        $('#editUserForm').on('afterValidate', function () {
          console.log("b");
        });


      },
      error: function (jqXHR, textStatus, errorThrown) {


      }
    });

  },
};
  InitEditUser: function (event) {

var target = $(event.target);
var id = target.attr('id');
var url = 'http://' + window.location.hostname + '/users/' + id;
var urlGet = window.location.href;

$。AJAX({       键入:' GET',       url:urlGet,       数据:' id =' + id,       success:function(data,textStatus,jqXHR){         的console.log(' OK&#39);          },       错误:function(jqXHR,textStatus,errorThrown){       }     });

$('#editUser').modal('show');


$.ajax({
  type: 'GET',
  url: url,
  success: function (data, textStatus, jqXHR) {
    console.log(data);
    $('#editUserInput').val(data.login);
    $('#editUserFullNameInput').val(data.full_name);
    $('#editUserEmailInput').val(data.email);

    $('#editUserForm').on('beforeSubmit', function () {
      console.log("a");
      return false;
    });
    $('#editUserForm').on('afterValidate', function () {
      console.log("b");
    });


  },
  error: function (jqXHR, textStatus, errorThrown) {

  }
});

}, };

0 个答案:

没有答案