2013-06-04 55 views
3

我想在我的symfony項目中使用MinLength驗證程序。Symfony2 - MinLength驗證不起作用 - MinLength未找到

這裏是我如何使用它:

use Symfony\Component\Validator\Constraints\MinLength; 
class RegisterNewUser 
{ 
    protected $password; 


    public static function loadValidatorMetadata(ClassMetadata $metadata) 
    { 
     if(isset($metadata->properties["password"])) 
      unset($metadata->properties["password"]); 


     $password_blank = new NotBlank(); 
     $password_min  = new MinLength(5); 


     $password_blank->message = "The password should not be blank"; 
     $password_min->message  = "The password is too short. It should have {{ limit }} characters or more"; 


     $metadata->addPropertyConstraint('password', $password_blank); 
     $metadata->addPropertyConstraint('password', $password_min); 
    } 
} 

我得到的錯誤信息是:

FatalErrorException: Error: Class 'Symfony\Component\Validator\Constraints\MinLength' not found in...

+0

@meze,謝謝,我剛剛在symfony網站上找到了這個。有MinLength,請參閱:http://symfony.com/doc/2.2/reference/constraints/MinLength.html –

回答

10

我已經找到了解決辦法:

從Symfony的文檔:

The MinLength constraint is deprecated since version 2.1 and will be removed in Symfony 2.3. Use Length with the min option instead.

所以,解決方案是:

use Symfony\Component\Validator\Constraints\Length; 

.... 

$password_min   = new Length(array('min'=>5)); 
相關問題