密码不在yii2中确认

时间:2016-02-10 16:21:41

标签: yii2 yii-extensions yii2-advanced-app

模型密码中的规则确认不起作用如何修复

public function rules()
{
    return [
        [['username', 'email'], 'filter', 'filter' => 'trim'],
        [['username', 'email', 'status','password','confirmpassword'], 'required'],
        ['email', 'email'],
        ['username', 'string', 'min' => 2, 'max' => 255],

        // password field is required on 'create' scenario

   ['confirmpassword', 'compare', 'compareAttribute'=>'password', 'skipOnEmpty' => false, 'message'=>"Passwords don't match"],

        // use passwordStrengthRule() method to determine password strength
        $this->passwordStrengthRule(),

        ['username', 'unique', 'message' => 'This username has already been taken.'],
        ['email', 'unique', 'message' => 'This email address has already been taken.'],
    ];
}

1 个答案:

答案 0 :(得分:0)

我认为你应该使用自定义验证而不是在规则返回数组中使用代码验证(我认为你在数组中使用的代码会破坏验证...)

 public function rules()
{
    return [
        [['username', 'email'], 'filter', 'filter' => 'trim'],
        [['username', 'email', 'status','password','confirmpassword'], 'required'],
        ['email', 'email'],
        ['username', 'string', 'min' => 2, 'max' => 255],

        // use a custom validation functio call 
        ['password', 'passwordStrengthRule']


        ['confirmpassword', 'compare', 'compareAttribute'=>'password', 'skipOnEmpty' => false, 'message'=>"Passwords don't match"],



        ['username', 'unique', 'message' => 'This username has already been taken.'],
        ['email', 'unique', 'message' => 'This email address has already been taken.'],
    ];
}

// function  for custom validation of password strenght
public function passwordStrengthRule($attribute, $params)
{

  //your password Strength Validation rule code 
  //eg: 
   return $this->passwordStrengthRule($attribute);

}

see this doc获取简短的输入验证指南