Repeat Password does not work in Yii2

StreetCoder picture StreetCoder · Mar 11, 2015 · Viewed 17.1k times · Source

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" ],
    ];
}

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.

Answer

arkoak picture arkoak · Mar 11, 2015

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" ],
    ];