Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - MinLength validation does not work - MinLength not found

I am trying to use th MinLength validator in my symfony project.

Here is how I am using it:

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 );
    }
}

The Error message I am getting is:

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

like image 889
Milos Cuculovic Avatar asked Dec 27 '22 02:12

Milos Cuculovic


1 Answers

I have found the solution:

From the Symfony documentation:

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

So, the solution is:

use Symfony\Component\Validator\Constraints\Length;

....

$password_min           = new Length(array('min'=>5));
like image 155
Milos Cuculovic Avatar answered Dec 29 '22 12:12

Milos Cuculovic