Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read annotation when use Symfony Validator as standalone

Tags:

symfony

I get this error:

Message: "[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Length" in property User::$name does not exist, or could not be auto-loaded."

This is the code on Github https://github.com/symfony/Validator

use Symfony\Component\Validator\Validation; 
use Symfony\Component\Validator\Constraints as Assert;

class User {
    /**
     * @Assert\Length(min = 3)
     * @Assert\NotBlank
     */
    private $name;

    /**
     * @Assert\Email
     * @Assert\NotBlank
     */
    private $email;

    public function __construct($name, $email)
    {
        $this->name = $name;
        $this->email = $email;
    }

    /**
     * @Assert\True(message = "The user should have a Google Mail account")
     */
    public function isGmailUser()
    {
        return false !== strpos($this->email, '@gmail.com');
    } 
}

$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$user = new User('John Doe', '[email protected]');

$violations = $validator->validate($user);

How can I fix this problem ?

like image 663
meotimdihia Avatar asked Sep 17 '12 09:09

meotimdihia


1 Answers

Doctrine don't use autoload PHP , you must register with autoloadRegistry:

AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "path/to/symfony/library/validator");
like image 62
meotimdihia Avatar answered Sep 22 '22 01:09

meotimdihia