Symfony2 Validation Datetime 1 should be before Datetime 2 Symfony2 Validation Datetime 1 should be before Datetime 2 php php

Symfony2 Validation Datetime 1 should be before Datetime 2


You could add a validation getter to the entity - Symfony2 Validation Getters

In your validation

Acme\YourBundle\Entity\Employment:    getters:        datesValid:            - "True": { message: "The start date must be before the end date" }

And then in your entity

public function isDatesValid(){    return ($this->startDate < $this->endDate);}


You could write a custom DateRangeValidator.

class DateRange extends Constraint {    public $message = "daterange.violation.crossing";    public $emptyStartDate = "daterange.violation.startDate";    public $emptyEndDate = "daterange.violation.endDate";    public $hasEndDate = true;    public function getTargets() {        return self::CLASS_CONSTRAINT;    }    public function validatedBy() {        return 'daterange_validator';    }}class DateRangeValidator extends ConstraintValidator{    public function isValid($entity, Constraint $constraint)    {        $hasEndDate = true;        if ($constraint->hasEndDate !== null) {            $hasEndDate = $constraint->hasEndDate;        }        if ($entity->getStartDate() !== null) {            if ($hasEndDate) {                if ($entity->getEndDate() !== null) {                    if ($entity->getStartDate() > $entity->getEndDate()) {                        $this->setMessage($constraint->message);                        return false;                    }                    return true;                } else {                    $this->setMessage($constraint->emptyEndDate);                    return false;                }            } else {                if ($entity->getEndDate() !== null) {                    if ($entity->getStartDate() > $entity->getEndDate()) {                        $this->setMessage($constraint->message);                        return false;                    }                }                return true;            }        } else {            $this->setMessage($constraint->emptyStartDate);            return false;        }    }

register it as a service:

parameters:    register.daterange.validator.class:     XXX\FormExtensionsBundle\Validator\Constraints\DateRangeValidatorservices:    daterange.validator:        class: %register.daterange.validator.class%        tags:            - { name: validator.constraint_validator, alias: daterange_validator }

And use it in your Entity:

use XXX\FormExtensionsBundle\Validator\Constraints as FormAssert;/** * * @FormAssert\DateRange() */class Contact extends Entity{    private $startDate;    private $endDate;}

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.


There is Yet Another Solution: Validation by using Expression Language:

use Symfony\Component\Validator\Constraints as Assert;/** * @ORM\Column(type="date", nullable=true) * @Assert\Date() */private $startDate;/** * @ORM\Column(type="date", nullable=true) * @Assert\Date() * @Assert\Expression( *     "this.getStartDate() < this.getEndDate()", *     message="The end date must be after the start date" * ) */private $endDate;

You can add this constraint to $startDate if you want.