我在模型中写了规则:
public $password_repeat;
/**
* @inheritdoc
*/
public function rules()
{
return [
....
....
['password', 'required'],
['password', 'string', 'min' => 6],
['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match" ],
];
}
Run Code Online (Sandbox Code Playgroud)
如果我使用不同的密码Password和Password Repeat领域,它提供了错误.所以,这意味着它有效.但问题是,如果Password Repeat字段为空,它不会给出任何错误.