重复密码在 Yii2 中不起作用 [英] Repeat Password does not work in Yii2

查看:25
本文介绍了重复密码在 Yii2 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中编写了如下规则:

I have written rules in the model as:

    public $password_repeat;

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        ....
        ....  
        ['password', 'required'],
        ['password', 'string', 'min' => 6],
        ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match" ],
    ];
}

如果我在 PasswordPassword Repeat 字段中使用不同的密码,则会出错.所以,这意味着它有效.但问题是,如果 Password Repeat 字段为空,它不会给出任何错误.

If I use different password in Password and Password Repeat field, it gives error. So, that's mean it works. But problem is that, it does not give any error if Password Repeat field is empty.

推荐答案

同时为 password_repeat 添加一个必需的标签.如下图

Add a required tag for password_repeat as well. Shown below

return [
        ....  
        ['password', 'required'],
        ['password', 'string', 'min' => 6],
        ['password_repeat', 'required'],
        ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match" ],
    ];

这篇关于重复密码在 Yii2 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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