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

标签 php forms validation symfony

我正在尝试在我的 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/

相关文章:

php - 如何检测网站是否在使用 IPAD 浏览

javascript - HTML 字段包含值时显示为空

css - html 网站首页弹出表单

javascript - 使用 javascript 验证 UNC 路径

Angular 2+ 自定义验证器 "required if"另一个字段已填写

PHP 按时间顺序显示

php - 如何将 php 脚本作为守护进程运行

PHP - 检查用户输入的日期是 future 的还是今天的

php - 检查 mysql 表中的 future 时间

php - 需要一个好的类的建议以用于表单显示和验证 (PHP)