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 ?
Doctrine don't use autoload PHP , you must register with autoloadRegistry:
AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "path/to/symfony/library/validator");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With