How to compare dates in validation?
Asked Answered
O

2

7

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

Obsession answered 20/4, 2016 at 12:48 Comment(4)
Possible duplicate of How to validate a property dependent on another property in Symfony 2Garett
I'm using the newer version of symfony. version 3Obsession
yes true, but that question was answered 4 years ago with a very ugly solution. I'm looking for the cleaner solution I'm sure symfony 3 can provide.Obsession
The /best/ solution is to use Expression validation, as stated in one of the answersAcroter
O
16

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;
Obsession answered 25/4, 2016 at 9:23 Comment(0)
E
-2

Use strtotime
You can convert the date format to timestap and then compare them both

Ekaterina answered 20/4, 2016 at 12:55 Comment(2)
You're mistaken. I'm using the symfony framework, which is a php framework.Obsession
I could if I wanted to just compare dates. But I'm locked into The Symfony-validation framework. I need to find out how to compare dates using annotations.Obsession

© 2022 - 2024 — McMap. All rights reserved.