phpsymfonydatesymfony-validator

Symfony constraint LessThan Date


I use Symfony v5.2 I've created this constraint :

$constraint = new Assert\Collection([
            'firstname' => [
                new Assert\NotNull(),
                new Assert\NotBlank()
            ],
            'lastname' => [
                new Assert\NotNull(),
                new Assert\NotBlank()
            ],
            'birthdate' => [
                new Assert\NotNull(),
                new Assert\NotBlank(),
                new Assert\Date(),
                new Assert\LessThan('today')
            ]
        ]);

Then I send this JSON with postman :

{
    "firstname": "john",
    "lastname": "doe",
    "birthdate": "2030-01-01"
}

But the assert is not triggered. It seems that the lessThan is only on DateTime, but not sure.

What I've done wrong ? Do i need to make my own check for a Date ?


Solution

  • In your case, 2 strings are compared by the LessThan validator

    You should convert the request value of birthdate to an object of DateTime before validation.