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; 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: ~
source share