gpt4 book ai didi

Symfony Assert\Expression 将实体属性与今天的日期进行比较

转载 作者:行者123 更新时间:2023-12-04 14:25:10 32 4
gpt4 key购买 nike

我想将 Assert\Expression 添加到我的实体的属性中。There should be an error message popping up, when the selected "Effective date"(this.getEffective()) is in the past or today.我只是想不通,如何将那个日期与今天的日期进行比较。

    /**
* @var boolean
* @ORM\Column(type="boolean", nullable=true)
* @Assert\Expression(
* "this.getEffective() > today",
* message="The effective date must be in the future!")
*/
private $status_stealth;

我对 nowdatetime.now() 进行了同样的尝试,并且我在谷歌上搜索了很多,但我没有发现任何人实际上比较过另一个断言表达式中包含当前日期的值。

想法?

最佳答案

默认情况下,表达式语法只支持一个函数,constant()。反过来,表达式验证器带有一个或两个变量(一个验证值和一个上下文对象)。您可以将值传递给实体的验证方法。

/**
* @Assert\Expression(expression="this.isStatusValid(value)")
*/
private $status;

public function isStatusValid($status) {
$currentDate = new \DateTimeImmutable();
return in_array($status, [1, 2, 3]) && $this->targetDate > $currentDate;
}

如果出现一些错误,只需调试即可。

public function isStatusValid($status) {
$currentDate = new \DateTimeImmutable();
var_dump($this->targetDate);
var_dump($currentDate);
die();
return in_array($status, [1, 2, 3]) && $this->targetDate > $currentDate;
}

文档链接:Expression Constraint , Expression Syntax .

关于Symfony Assert\Expression 将实体属性与今天的日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47415216/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com