Yii2 form validation - compare password repeat only when password field is filled
问题 My form validation uses the following rules: [['password', 'password_repeat'], 'required'], ['password_repeat', 'compare', 'compareAttribute' => 'password', 'message' => "Passwords don't match"], How to write rules for password_repeat to compare it with password only if user fill password field. If user skip password , validation for password_repeat should be also skipped. 回答1: You can use scenarios for that: public function rules() { return [ [['username', 'password'], 'required', 'on' =>