Mil*_*loš 3 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 );
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误消息是:
FatalErrorException:错误:在'...中找不到类'Symfony\Component\Validator\Constraints\MinLength'
Mil*_*loš 10
我找到了解决方案:
从Symfony文档中:
MinLength约束自版本2.1以来已弃用,将在Symfony 2.3中删除.使用长度和最小选项代替.
所以,解决方案是:
use Symfony\Component\Validator\Constraints\Length;
....
$password_min = new Length(array('min'=>5));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5289 次 |
最近记录: |