Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare dates in validation?

I'm trying to compare dates in my validation. The documentation says it's possible but It's not documented. I'm using annotations and I want one date to be later that the other. How do I do this?

enter image description here

like image 467
SnelleJelle Avatar asked Apr 20 '16 12:04

SnelleJelle


People also ask

How do you compare dates in validation rule?

Try using CompareTwoValues; it is multi-purpose. CompareDates(Date1+"T000000. 000 GMT", Date2+"T000000.

How do I compare two dates in Salesforce validation?

you can use the DATEVALUE(datetime field) function to extract the date part from the date/time field and then compare it with values like TODAY() - 1 or any specific date value ... you can use DATE(year, month, day) function to build a static date to compare..


1 Answers

I've eventually solved it using expressions like so:

     /**
     * @var \Datetime
     * @Assert\Type(
     *      type = "\DateTime",
     *      message = "vacancy.date.valid",
     * )
     * @Assert\GreaterThanOrEqual(
     *      value = "today",
     *      message = "vacancy.date.not_today"
     * )
     */
    private $startdate;

    /**
     * @var \Datetime
     * @Assert\Type(
     *      type = "\DateTime",
     *      message = "vacancy.date.valid",
     * )
     * @Assert\GreaterThanOrEqual(
     *      value = "today",
     *      message = "vacancy.date.not_today"
     * )
     * @Assert\Expression(
     *     "this.getEnddate() >= this.getStartdate()",
     *     message="vacancy.date.not_more_than"
     * )
     */
    private $enddate;
like image 59
SnelleJelle Avatar answered Nov 15 '22 07:11

SnelleJelle