我想用Symfony\Component\Validator\Constraints
和Doctrine
在我的控制器中我有
$user->setMyDate(new \DateTime('tomorrow'));
并在我的实体中
/**
* @var \DateTime
* @Assert\NotBlank()
* @Assert\LessThanOrEqual("today")
* @Assert\Range(
* min = "now",
* max = "now"
* )
* @Assert\DateTime()
* @ORM\Column(name="my_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $my_date;
我尝试使用LessThanOrEqual并使用Range,但它不起作用
答案 0 :(得分:0)
这是一个问题还是一个声明? 不,他们没有。
当初始化任何约束时,它会从传递的选项中获取要比较的值。在你的情况下 - 它的字符串"今天"或者"现在"。
因此,当应用约束时,字符串将与\ DataTime对象进行比较,显然,它不起作用。
但是要得到你需要的东西,你只需要创建一个自定义约束。
这样的事情:
class RageDateTime extends Range
{
public $min;
public $max;
public function __construct($options = null)
{
parent::__construct($options);
$this->max = new \DateTime($this->max);
$this->min = new \DateTime($this->min);
}
public function validatedBy()
{
return 'RangeValidator';
}
}