Php 在yii2中通过ajax验证表单

Php 在yii2中通过ajax验证表单,php,yii2,yii2-validation,Php,Yii2,Yii2 Validation,我有一个在弹出窗口中打开的表单,所以我想通过ajax验证来验证我的表单,但是当我点击提交按钮时,我的页面会刷新,所以我没有收到任何验证错误 查看文件: <?php $form = ActiveForm::begin([ 'id' => 'signup-form', 'enableAjaxValidation' => true, //'action' => Url::toRoute('user/ajaxregistratio

我有一个在弹出窗口中打开的表单,所以我想通过ajax验证来验证我的表单,但是当我点击提交按钮时,我的页面会刷新,所以我没有收到任何验证错误

查看文件:

 <?php $form = ActiveForm::begin([
        'id' => 'signup-form',
        'enableAjaxValidation' => true,
        //'action' => Url::toRoute('user/ajaxregistration'),
        'validationUrl' => Url::toRoute('user/ajaxregistration')

]); ?>

 <div class="col-md-12">
                    <div class="formbox">
                      <div class="inputbox signup">
                        <div class="input-group"> <span class="input-group-addon"><i class="glyphicon name"></i></span>
                          <?= Html::textInput('userFullName', '', ['placeholder' => "Name",'class'=>'form-control']); ?>
                        </div>

 <?php ActiveForm::end(); ?>
型号代码:

 return [

            ['userFullName', 'trim'],
            ['userFullName', 'required'],


        ];
请建议我应该怎么做,以使我的页面不会得到refrsh,并得到验证错误

使用renderAjax()方法:


您使用的ActiveForm没有任何ActiveFields,这意味着在模型中定义的验证规则甚至没有分配给文本输入。我已经为您的问题编写了一个实现:

型号:

use Yii;
use yii\base\Model;

class ExampleForm extends Model
{
    // this 'virtual attribute' defines a form field
    public $userFullName;

    // validation rules
    public function rules()
    {
        return [
            ['userFullName', 'trim'],
            ['userFullName', 'required'],
        ];
    }

}
控制器:

use models\ExampleForm;
use yii\web\Response;
use yii\widgets\ActiveForm;

public function actionExample()
{
    $model = new ExampleForm;

    // validate any AJAX requests fired off by the form
    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())) {

        // code to process form data goes here.. This will execute on a form submission.

    }

    return $this->render('example-form', [
        'model' => $model,
    ]);
}
视图:


请填写这张表格

查看ajax请求是否被发送/表单是否被验证的最佳方法是检查chromes developer工具,转到网络选项卡并检查活动

use Yii;
use yii\base\Model;

class ExampleForm extends Model
{
    // this 'virtual attribute' defines a form field
    public $userFullName;

    // validation rules
    public function rules()
    {
        return [
            ['userFullName', 'trim'],
            ['userFullName', 'required'],
        ];
    }

}
use models\ExampleForm;
use yii\web\Response;
use yii\widgets\ActiveForm;

public function actionExample()
{
    $model = new ExampleForm;

    // validate any AJAX requests fired off by the form
    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())) {

        // code to process form data goes here.. This will execute on a form submission.

    }

    return $this->render('example-form', [
        'model' => $model,
    ]);
}
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;

$this->title = 'Example';
?>
<div class="exampleForm">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>Please fill out the form.</p>

    <!-- this form will submit via POST to the same action that renders it -->
    <?php $form = ActiveForm::begin() ?>

        <!-- this is an active field. Any validation rules for the 'userFullName' field -->
        <!-- that have been defined within the $model object will be applied to this field -->
        <!-- the validation has also been set to validate via ajax within the $options array -->
        <!-- read more about ActiveFields here: http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html -->
        <?= $form->field($model, 'userFullName', ['enableAjaxValidation' => true]) ?>

        <div class="form-group">
            <?= Html::submitButton('Submit!', ['class' => 'btn btn-primary']) ?>
        </div>

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

</div>