Yii2 activeform ajax submit and validation

百般思念 提交于 2019-12-03 00:49:24

Use beforeSubmit event instead of submit, the beforeSubmit will only be triggered once the form passes the validation.

$('form').on('beforeSubmit', function(e) {
    var form = $(this);
    var formData = form.serialize();
    $.ajax({
        url: form.attr("action"),
        type: form.attr("method"),
        data: formData,
        success: function (data) {
             ...
        },
        error: function () {
            alert("Something went wrong");
        }
    });
}).on('submit', function(e){
    e.preventDefault();
});

you can use AjaxSubmitButton.

Your form should b

$form = ActiveForm::begin([
    'id' => "some_form",
    'action' => 'javascript:void(0)',
    'options' => ['class' => 'edit_form'],
]);

use AjaxSubmitButton here

AjaxSubmitButton::begin([
                                'label' => 'Submit',
                                'id' => 'some_form',
                                'ajaxOptions' => [
                                    'type' => 'POST',
                                    'url' => \yii\helpers\Url::to(['/user/edit']),
                                    'success' => new \yii\web\JsExpression(
                                            'function(data){
                                                if(data=="success")
                                                    {
                                                    }else{
                                                        $.each(data, function(key, val) {
                                                            $("#"+key).after("<div class=\"help-block\">"+val+"</div>");
                                                            $("#"+key).closest(".form-group").addClass("has-error");
                                                        });
                                                    }
                                                }'
                                    ),
                                ],
                                'options' => ['class' => 'btn btn-success', 'type' => 'submit'],
                            ]);
                            AjaxSubmitButton::end(); 

In your controller

public function actionEdit()
{
     $model = new User;
     if($model->save())
     {
        $result = 'success';
        Yii::$app->response->format = trim(Response::FORMAT_JSON);
                return $result;
     }else{
         $error = \yii\widgets\ActiveForm::validate($model);
                Yii::$app->response->format = trim(Response::FORMAT_JSON);
                return $error; 
     }
}

Here I've find some interesting javascript-side validation tricks

So, a javascript submit button can be as:

$('body').on('click', '#submit', function(e) {
  e.preventDefault();
  var yiiform = $('#my-form');
  $.ajax({
    type: yiiform.attr('method'),
      url: yiiform.attr('action'),
      data: yiiform.serializeArray(),
      success: function(data) {
        if(data.success == 'true') {
          window.location.href = 'http://my.success.page';
        } else {
          // here there is (maybe) the right way to trigger errors
          $.each(data, function(key, val) {
            yiiform.yiiActiveForm('updateAttribute', key, [val]);
          });
        }
      }
  });
}

triggered by:

<?= Button::widget([
    'label' => Yii::t('app', 'Submit'),
     'options' => [
        'id'=>'submit',
        'class' => 'btn btn-primary pull-right',
     ]]);?>

And the action controller will reply with:

...
if ($model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    if($model->save()) {
        return ['success'=>'true'];
    } else {
        return ActiveForm::validate($model);
    }
}
...

Details about ActiveForm::validate() can be found here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!