Search code examples
annotationssymfonysymfony-validator

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


Solution

  • 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;