2012-03-19 96 views
0

我的問題是「我如何在帶有註釋的實體中形成重複驗證?」。我有一個帳戶實體(電子郵件,密碼和confirmPassword)屬性。當新用戶註冊新帳戶時,他/她必須填寫電子郵件,密碼和confirmPassword字段。顯然,密碼和confirmPassword字段必須匹配。我在下面的Stachoverflow中看到了使用純php(表單生成器)進行此驗證的示例。Symfony 2在實體中形成帶註釋的重複驗證

$builder->add('password', 'repeated', array(
    'type' => 'password', 
    'first_name' => 'Password', 
    'second_name' => 'Password confirmation', 
    'invalid_message' => 'Passwords are not the same', 
)); 

但是,這不是我想要的。我希望在我的帳戶實體中使用註釋功能。也許

* @Assert\Match(
*  matchField = "password", 
*  message = "The password confirmation does not match password." 
*) 
protected $confirmPassword; 
+0

爲什麼要在實體中使用$ confirmPassword? – gremo 2012-03-20 07:27:54

回答

1

您可以使用方法驗證:

/** 
* @Assert\True(message = "Passwords are not the same") 
*/ 
public function isPasswordLegal() 
{ 
    return ($this->password == $this->confirmPassword); 
}