gpt4 book ai didi

php - Symfony2 - MinLength 验证不起作用 - 未找到 MinLength

转载 作者:可可西里 更新时间:2023-11-01 00:05:48 26 4
gpt4 key购买 nike

我正在尝试在我的 symfony 项目中使用 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...

最佳答案

我找到了解决方案:

来自 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));

关于php - Symfony2 - MinLength 验证不起作用 - 未找到 MinLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16918175/

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