Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Validator Constraint GreaterThan on other property

My validations are defined in a yaml file like so;

# src/My/Bundle/Resources/config/validation.yml
My\Bundle\Model\Foo:
    properties:
        id:
            - NotBlank: 
                groups: [add]
        min_time:
            - Range:
                min: 0
                max: 99
                minMessage: "Min time must be greater than {{ limit }}"
                maxMessage: "Min time must be less than {{ limit }}"
                groups: [add]
        max_time:
            - GreaterThan:
                value: min_time
                groups: [add]

How do I use the validator constraint GreaterThan to check against another property?
E.g make sure max_time is greater than min_time?

I know I can create a custom constraint validator, but surely you can do it using the GreaterThan constraint.
Hopefully I am missing something really simple here

like image 459
Rooneyl Avatar asked Jan 15 '14 13:01

Rooneyl


1 Answers

Try GreaterThan constraint with option propertyPath:

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Column(type="datetime", nullable=true)
 * @Assert\DateTime()
 * @Assert\GreaterThan(propertyPath="minTime")
 */
 protected $maxTime;
like image 185
Alexander Sholk Avatar answered Oct 17 '22 00:10

Alexander Sholk