【问题标题】:Symfony2 - MinLength validation does not work - MinLength not foundSymfony2 - MinLength 验证不起作用 - MinLength not found
【发布时间】:2013-05-30 20:51:40
【问题描述】:

我正在尝试在我的 symfony 项目中使用 th 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:错误:在...中找不到类“Symfony\Component\Validator\Constraints\MinLength”

【问题讨论】:

标签: php forms validation symfony


【解决方案1】:

我找到了解决办法:

来自 Symfony 文档:

MinLength 约束自 2.1 版以来已被弃用,并将在 Symfony 2.3 中删除。请改用 Length 和 min 选项。

所以,解决办法是:

use Symfony\Component\Validator\Constraints\Length;

....

$password_min           = new Length(array('min'=>5));

【讨论】:

    【解决方案2】:

    长度也被弃用了

    php.INFO: User Deprecated: Using the "Symfony\Component\Validator\Constraints\Length" 约束和 "min" 未设置“allowEmptyString”的选项已弃用,默认为 true。在 5.0 中,它将成为可选的 并默认为 false。

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2016-10-27
      • 2013-09-02
      • 1970-01-01
      • 2022-11-05
      • 2022-10-08
      相关资源
      最近更新 更多