Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Validation Datetime 1 should be before Datetime 2

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;

...
}
like image 689
Simon Avatar asked Apr 12 '13 13:04

Simon


3 Answers

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);
}
like image 58
qooplmao Avatar answered Oct 23 '22 14:10

qooplmao


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.

like image 33
Stephan Vierkant Avatar answered Oct 23 '22 15:10

Stephan Vierkant


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\DateRangeValidator

services:
    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.

like image 35
DerStoffel Avatar answered Oct 23 '22 14:10

DerStoffel