Skip to content

Instantly share code, notes, and snippets.

@binabik
Created August 9, 2012 08:11
Show Gist options
  • Save binabik/3302245 to your computer and use it in GitHub Desktop.
Save binabik/3302245 to your computer and use it in GitHub Desktop.
Custom Validator not being called
<?php
namespace SimonB\CMSBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @author simon
* @Annotation
*/
class FutureDate extends Constraint
{
public $message = 'The date "%date%" must be in the future.';
}
<?php
namespace SimonB\CMSBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* @author simon
*/
class FutureDateValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$this->context->addViolation("SIMON GOT SOMETHING WRONG!");
// $now = new \DateTime();
// if (! $value instanceof \DateTime)
// $value = new \DateTime($value);
// if ($value < $now) {
// $this->context->addViolation($constraint->message, array('%date%' => $value));
// }
}
}
<?php
namespace SimonB\CMSBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use SimonB\CMSBundle\Validator\Constraints as SBAssert;
/**
* @ORM\Table()
* @ORM\Entity()
*/
class Tile
{
// <SNIP>
/**
* @var string $contentText
* @ORM\Column(type="text", nullable=true)
* @SBAssert\FutureDate
*/
private $contentText;
// <SNIP>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment