Yii2登录表单仅在ajaxvalidation为false时有效

时间:2017-08-09 06:20:14

标签: yii2

我有一个登录表单,只有当我转动ajaxValidation. If i enable it enableAjaxValidation = true`它才会生效。问题是什么?它与模型规则有关吗?在之前的项目中,我使用相同的形式,一切都很好。无法实现。你们能提出建议和快速解释吗?提前谢谢!

<div class="row">
    <div class="col-xs-12">
        <h3 class="contact-page-title"><?= Yii::t('app', 'app.Login') ?></h3>
<?php if (Yii::$app->user->isGuest): ?>
    <?php $form = ActiveForm::begin([
        'id'                     => 'login-widget-form',
        'action'                 => Url::to(['/user/security/login']),
        /*'enableAjaxValidation'   => true,*/
        'enableClientValidation' => false,
        'validateOnBlur'         => false,
        'validateOnType'         => false,
        'validateOnChange'       => false,
    ]) ?>

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

    <?= $form->field($model, 'password')->passwordInput() ?>

    <?= $form->field($model, 'rememberMe')->checkbox() ?>

        <input type="hidden" name="checkoutLogin" value="1">


    <?= Html::submitButton(Yii::t('user', Yii::t('app','app.Login')) , ['class' => 'btn-1 shadow-0 full-width']) ?>

    <?php ActiveForm::end(); ?>
<?php else: ?>
    <?= Html::a(Yii::t('user', 'Logout'), ['/user/security/logout'], [
        'class'       => 'btn btn-danger btn-block',
        'data-method' => 'post'
    ]) ?>
<?php endif ?>
    </div>
</div>

这些是规则:

public function rules()
    {
        $rules = [
            'loginTrim' => ['login', 'trim'],
            'requiredFields' => [['login'], 'required'],
            'confirmationValidate' => [
                'login',
                function ($attribute) {
                    if ($this->user !== null) {
                        $confirmationRequired = $this->module->enableConfirmation
                            && !$this->module->enableUnconfirmedLogin;
                        if ($confirmationRequired && !$this->user->getIsConfirmed()) {
                            $this->addError($attribute, Yii::t('user', 'You need to confirm your email address'));
                        }
                        if ($this->user->getIsBlocked()) {
                            $this->addError($attribute, Yii::t('user', 'Your account has been blocked'));
                        }
                    }
                }
            ],
            'rememberMe' => ['rememberMe', 'boolean'],
        ];

        if (!$this->module->debug) {
            $rules = array_merge($rules, [
                'requiredFields' => [['login', 'password'], 'required'],
                'passwordValidate' => [
                    'password',
                    function ($attribute) {
                        if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) {
                            $this->addError($attribute, Yii::t('user', 'Invalid login or password'));
                        }
                    }
                ]
            ]);
        }

        return $rules;
    }

编辑行动:

public function actionLogin() {
        if (!Yii::$app->user->isGuest) {
            $this->goHome();
        }
        $register = new RegistrationForm();
        if(Yii::$app->request->isAjax){
            return 1;
        }
        /** @var LoginForm $model */
        $model = Yii::createObject(LoginForm::className());
        $event = $this->getFormEvent($model);

        $this->performAjaxValidation($model);
        $this->trigger(self::EVENT_BEFORE_LOGIN, $event);

        if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
            $this->trigger(self::EVENT_AFTER_LOGIN, $event);
            //return $this->goBack();
            if (Yii::$app->request->baseUrl == "" and !isset($_POST["checkoutLogin"])) {

                $session = Yii::$app->session;
                return $this->redirect('/');
            } elseif(isset($_POST["checkoutLogin"]) and $_POST["checkoutLogin"] == 1) {
                $lang = \frontend\models\Lang::getCurrent();
                $checkout = Page::findOne(91);
                if($checkout){
                    return $this->redirect('/'.$lang->url.'/'.$checkout->url);
                }else{
                   return $this->goBack(); 
                }
            } else {
                return $this->goBack();
            }
        }

        return $this->render('login', [
                    'model' => $model,
                    'module' => $this->module,
                    'register' => $register
        ]);
    }

1 个答案:

答案 0 :(得分:0)

尝试将performAjaxValidation()的代码移至动作:

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