在yii2

时间:2016-11-03 17:51:36

标签: php yii2 yii2-validation

我有一个弹出窗口打开的表单,所以我想通过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(); ?>

控制器文件:

public function actionValidate() {

    $model = new SignupForm;

    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model); \Yii::$app->end();

    }


}

型号代码:

 return [

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


        ];

请建议我该怎么办才能使我的页面不会变得苛刻,我会收到验证错误

2 个答案:

答案 0 :(得分:3)

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

型号:

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>

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

答案 1 :(得分:1)

使用renderAjax()方法:

 if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model);

 }else {
            return $this->renderAjax('YourViewFileName', [
                'model' => $model,
            ]);
        }