问题描述
我使用 Symfony v5.2 我已经创建了这个约束:
$constraint = new Assert\Collection([
'firstname' => [
new Assert\NotNull(),new Assert\NotBlank()
],'lastname' => [
new Assert\NotNull(),'birthdate' => [
new Assert\NotNull(),new Assert\NotBlank(),new Assert\Date(),new Assert\Lessthan('today')
]
]);
然后我用邮递员发送这个 JSON :
{
"firstname": "john","lastname": "doe","birthdate": "2030-01-01"
}
但是断言没有被触发。似乎 lessthan 只在 DateTime 上,但不确定。
我做错了什么?我需要自己检查日期吗?
解决方法
在您的情况下,LessThan 验证器比较了 2 个字符串
您应该在验证之前将 birthdate
的请求值转换为 DateTime
的对象。