Yii2 - ActiveForm ajax提交

Văn*_*yễn 27 ajax yii2 active-form

我如何使用ActiveForm满足这些要求?

  • 使用ajax提交表单.

  • 在使用ajax提交之前:检查是否有错误退出.

  • 提交后:如果服务器响应不成功保存结果,则在字段输入下显示字段错误.

Har*_*ari 25

这是您的表单.我更喜欢使用不同的操作进行验证和保存.您可以将它们加入到单个方法中.

<?php $form = \yii\widgets\ActiveForm::begin([
    'id' => 'my-form-id',
    'action' => 'save-url',
    'enableAjaxValidation' => true,
    'validationUrl' => 'validation-rul',
]); ?>

<?= $form->field($model, 'email')->textInput(); ?>

<?= Html::submitButton('Submit'); ?>
<?php $form->end(); ?>
Run Code Online (Sandbox Code Playgroud)

在验证操作中,您应该写.它验证您的表单并将错误列表返回给客户端.:

public function actionValidate()
{
    $model = new MyModel();
    $request = \Yii::$app->getRequest();
    if ($request->isPost && $model->load($request->post())) {
        \Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是保存动作.在验证安全性输入数据时:

public function actionSave()
{
    $model = new MyModel();
    $request = \Yii::$app->getRequest();
    if ($request->isPost && $model->load($request->post())) {
        \Yii::$app->response->format = Response::FORMAT_JSON;
        return ['success' => $model->save()];
    }
    return $this->renderAjax('registration', [
        'model' => $model,
    ]);
}
Run Code Online (Sandbox Code Playgroud)

此代码将在actionValidate()和中验证您的表单.要通过AJAX提交表单,请使用beforeSubmit事件.在你的javascript文件中写道:

$(document).on("beforeSubmit", "#my-form-id", function () {
    // send data to actionSave by ajax request.
    return false; // Cancel form submitting.
});
Run Code Online (Sandbox Code Playgroud)

就这样.


小智 9

使用ajax提交表单.在使用ajax提交之前:检查是否有错误退出.yii默认显示错误....... :)

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;

/* @var $this yii\web\View */
/* @var $model backend\models\search\JobSearch */
/* @var $form yii\bootstrap\ActiveForm */
?>

<div class="job-search">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        //'method' => 'get',
        'options' => ['id' => 'dynamic-form111']
    ]); ?>

    <?php echo $form->field($searchModel, 'id') ?>

    <?php echo $form->field($searchModel, 'user_id') ?>

    <?php echo $form->field($searchModel, 'com_id') ?>

    <?php echo $form->field($searchModel, 'job_no') ?>

    <?php echo $form->field($searchModel, 'court_id') ?>

    <?php // echo $form->field($model, 'case_no') ?>

    <?php // echo $form->field($model, 'plainttiff') ?>

    <?php // echo $form->field($model, 'defendant') ?>

    <?php // echo $form->field($model, 'date_fill') ?>

    <?php // echo $form->field($model, 'court_date') ?>

    <?php // echo $form->field($model, 'status_id') ?>

    <?php // echo $form->field($model, 'created_at') ?>

    <?php // echo $form->field($model, 'updated_at') ?>

    <div class="form-group">
        <?php echo Html::submitButton('Search', ['class' => 'btn btn-primary','id'=>'submit_id']) ?>
        <?php echo Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>  
<script type="text/javascript">
    $(document).ready(function () {
        $('body').on('beforeSubmit', 'form#dynamic-form111', function () {
            var form = $(this);
            // return false if form still have some validation errors
            if (form.find('.has-error').length) 
            {
                return false;
            }
            // submit form
            $.ajax({
            url    : form.attr('action'),
            type   : 'get',
            data   : form.serialize(),
            success: function (response) 
            {
                var getupdatedata = $(response).find('#filter_id_test');
                // $.pjax.reload('#note_update_id'); for pjax update
                $('#yiiikap').html(getupdatedata);
                //console.log(getupdatedata);
            },
            error  : function () 
            {
                console.log('internal server error');
            }
            });
            return false;
         });
    });
</script>
Run Code Online (Sandbox Code Playgroud)