Yii2唯一验证器被忽略 [英] Yii2 unique validator ignored

查看:319
本文介绍了Yii2唯一验证器被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的RegisterForm模型的rules()中:

In the rules() of my RegisterForm model:

[ 'user_username', 'unique', 'targetClass' => 'app\models\User', 'message' => 'This username is already been taken.' ],

在我的控制器中:

$model = new RegisterForm();
if ( $model->load( Yii::$app->request->post() ) ) {
    if ( $user = $model->register() ) {
        return $this->redirect( [ '/login' ] );
    }
}

在RegisterForm中:

In RegisterForm:

public function register() {  
    $user = new User();
    $user->user_firstname = $this->user_firstname;
    $user->user_lastname = $this->user_lastname;
    $user->user_username = $this->user_username;
    $user->user_email = $this->user_email;
    $user->setPassword( $this->user_password );

    if ( !$user->validate() ) {
        return null;
    }    

    if ( $user->save() ) {
        return $user;   
    }

    return null;
}

表格:

<?php $form = ActiveForm::begin(); ?>

<?= $form->field( $model, 'user_firstname' )->textInput( [ 'maxlength' => true ] ) ?>

<?= $form->field( $model, 'user_lastname' )->textInput( [ 'maxlength' => true ] ) ?>

<?= $form->field( $model, 'user_username' )->textInput( [ 'maxlength' => true ] ) ?>

<?= $form->field( $model, 'user_email' )->textInput( [ 'maxlength' => true ] ) ?>

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

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

<?= Html::submitButton( 'Register', [ 'class' => 'btn btn-primary', 'name' => 'register-button' ] ) ?>

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

但是,当我输入一个我知道已经存在的用户名时,虽然我得到了Integrity constraint violation: 1062 Duplicate entry ...

Yet when I enter a username that I know already exists, the error never comes up and the record tries to save, though I get: Integrity constraint violation: 1062 Duplicate entry ...

如果我向用户模型本身添加唯一规则,则如果输入存在的用户名,表单将不会提交,错误不会显示

if I add the unique rule to the User model itself the form will not submit if I input a username that exists, the errors just don't show up

推荐答案

就像我怀疑的那样,您没有在客户端检查唯一的user_username属性.它不起作用的原因是因为您没有发送Ajax请求来检查数据库的结果.与其他规则不同,unique规则需要向服务器发送额外的Ajax请求,因为如果Javascript检索所有当前已注册的用户名并将其存储在客户端中,这将是一件很糟糕的事情.

Like I have suspected, you are not checking for unique user_username attribute in client side. The reason why it's not working it's because you are not sending Ajax requests to check the results from database. Unlike other rules, unique rule requires additional Ajax requests to server since it would be pretty bad thing if Javascript would retrieve all currently registered username and store somewhere in Client side.

要解决您的问题,请在表单中输入以下内容:

To solve you problem, in form write something like this:

$form = ActiveForm::begin([
    'enableAjaxValidation' => true,
    'validationUrl' => [<URL HERE>],
]);

现在,您必须在控制器中创建一个方法(操作),以将验证(不仅是唯一的,而且是所有验证)返回到ActiveForm.所以可能是这样的:

Now you have to create a method (action) in controller that returns validation (not just unique, all of them) back to ActiveForm. So it could be something like this:

public function actionAjaxValidation()
{
    $post = Yii::$app->request->post();
    $model = new YourClass();

    if (!$model->load($post)) {
        throw new HttpException(403, 'Cannot load model');
    }

    $array = ActiveForm::validate($model);

    return json_encode($array);
}

这篇关于Yii2唯一验证器被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆