比较规则yii2无法正常工作

时间:2016-04-08 13:53:02

标签: php validation yii yii2 client-side-validation

我有这样的规则方法:

public function rules()
{
    return [
        [['username', 'email', 'password'],'filter', 'filter' => 'trim'],
        [['username', 'email', 'password'],'required', 'message' => '{attribute} can not be empty'],
        ['username', 'string', 'min' => 2, 'max' => 255],
        ['password', 'string', 'min' => 6, 'max' => 255],
        ['password_repeat', 'required', 'message' => 'This field can not be empty'],
        ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match", 'skipOnError' => true],
        ['username', 'unique',
            'targetClass' => User::className(),
            'message' => 'This name is already used.'],
        ['email', 'email'],
        ['email', 'unique',
            'targetClass' => User::className(),
            'message' => 'This name is already used.'],
    ];
}

我的观看代码是这样的:

<?php $form = ActiveForm::begin(['action' => 'login/register']); ?>
        <?= $form->field($registration, 'username',
            ['template' => '<div class="uk-form-row">
                                {input}{error}
                                </div>'])
            ->textInput(['id' => 'register_username', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'password',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput(['id' => 'register_password', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'password_repeat',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput(['id' => 'register_password_repeat', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'email',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->textInput(['id' => 'register_email', 'class' => 'md-input']) ?>
        <div class="uk-margin-medium-top">
            <button class="md-btn md-btn-primary md-btn-block md-btn-large">Sign in</button>
        </div>
        <?php ActiveForm::end(); ?>

当我填写所有给定字段时,即使第一次更正密码,我也会在重复密码时遇到错误Passwords don't match。是否存在我的验证规则,或者它是Yii Validator中的错误?

UPD:我已经尝试了'skipOnError' => true。我发现它是类似问题的答案,但它仍然无法正常工作。

UPD:我在我的控制台中做了一些验证:

var a = $('#register_password')
undefined
a.val()
"Halloha"
var b = $('#register_password_repeat')
undefined
b.val()
"Halloha"

但它仍显示Passwords don't match错误消息

1 个答案:

答案 0 :(得分:2)

尝试使用像这样的规则

from tabulate import tabulate
class student:
    def __init__(self, code, name, number):
        self.code = code
        self.name = name
        self.number = number
        self.stu = [self.code, self.name, self.number]

students = []
s1 = student("901", "Joh Doe", "123456")
s2 = student("902", "Mary Mount", "566665")
s3 = student("903", "David Lee", "394624")

students.append(s1.stu)
students.append(s2.stu)
students.append(s3.stu)

print(tabulate(students))

它会自动将密码值与属性password_repeat进行比较,而不是按照其他顺序进行,如文档中所述。

http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#compare