I'm looking through the Symfony2 Validation Reference but I'm not finding what I need.
I have a class Employment with a StartDate and EndDate. I would like to add an \@Assert() where it verifies that StartDate is always BEFORE EndDate. Is there a standard way of comparing class attributes as a Validation Constraint or should I create a custom validation constraint?
class Employment {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose()
*/
protected $id;
/**
* @ORM\Column(type="datetime")
* @Expose()
* @Assert\DateTime()
*/
protected $startDate;
/**
* @ORM\Column(type="datetime", nullable=TRUE)
* @Expose()
* @Assert\DateTime()
*/
protected $endDate;
...
}
You could write a custom DateRangeValidator.
register it as a service:
And use it in your Entity:
Eventhough it seems a bit much for a simple thing like that, but experience shows, that one needs a date range validator more often than just once.
You could add a validation getter to the entity - Symfony2 Validation Getters
In your validation
And then in your entity
There is Yet Another Solution: Validation by using Expression Language:
You can add this constraint to
$startDate
if you want.