gpt4 book ai didi

symfony - 将 Symfony Validator 用作独立时无法读取注释

转载 作者:行者123 更新时间:2023-12-04 14:15:55 25 4
gpt4 key购买 nike

我收到此错误:

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



这是 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', 'john@example.com');

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

我该如何解决这个问题?

最佳答案

Doctrine 不使用 autoload PHP ,您必须使用 autoloadRegistry 注册:

AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "path/to/symfony/library/validator");

关于symfony - 将 Symfony Validator 用作独立时无法读取注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12456938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com