Symfony2 GreaterThan validator delimiter on another property

My checks are defined in the yaml file, for example;

# 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 to use GreaterThan validator GreaterThan to check for another property?
For example, make sure max_time is greater than min_time?

I know that I can create a custom constraint checking mechanism, but you can do it with a GreaterThan constraint.
Hope I missed something really simple.

+6
source share
3 answers

I suggest you take a look at the Custom Validator , especially Class Conformity Check .

I will not copy all the code, just the parts that you have to change.

SRC / My / Bundle \ Model \ Foo / Validator / Limitations / CheckTime.php

Define a validator, min_time and max_time are the 2 fields you want to check.

 <?php namespace My\Bundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; /** * @Annotation */ class CheckTime extends Constraint { public $message = 'Max time must be greater than min time'; public function validatedBy() { return 'CheckTimeValidator'; } public function getTargets() { return self::CLASS_CONSTRAINT; } } 

SRC / My / Bundle / Validator / Limitations / CheckTimeValidator.php

Define a validator:

 <?php namespace My\Bundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; class CheckTimeValidator extends ConstraintValidator { public function validate($foo, Constraint $constraint) { if ($foo->getMinTime() > $foo->getMaxTime()) { $this->context->addViolationAt('max_time', $constraint->message, array(), null); } } } 

SRC / My / Bundle / Resources / Configurations / validation.yml

Use the validator:

 My\Bundle\Entity\Foo: constraints: - My\Bundle\Validator\Constraints\CheckTime: ~ 
+2
source

I suggest using the Callback validator. Custom validators work too, but probably brute force if it is a one-time check and will not be reused in other parts of the application.

With the help of callback validators, you can define them on one object / model and be called at the class level.

In your case

 // in your My\Bundle\Model\Foo: /** * @Assert\Callback */ public function checkMaxTimeGreaterThanMin($foo, Constraint $constraint) { if ($foo->getMinTime() > $foo->getMaxTime()) { $this->context->addViolationAt('max_time', $constraint->message, array(), null); } } 

or if you use yaml

 My\Bundle\Model\Foo: constraints: - Callback: [checkMaxTimeGreaterThanMin] 
+2
source

Try GreaterThan with the propertyPath option:

 use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Column(type="datetime", nullable=true) * @Assert\DateTime() * @Assert\GreaterThan(propertyPath="minTime") */ protected $maxTime; 
0
source

All Articles