Reputation: 20223
I have a custom validator and I would like to access the entire entity from the validator.
I have found Class Constraint Validator http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#class-constraint-validator but I don't know how to use it.
Where to setup the validator, for the moment I have it like that:
$metadata->addPropertyConstraint('doi', new IsDOI());
But this si only for the parameter, not for the entire class. I can't really understand the symfony example.
Upvotes: 0
Views: 2262
Reputation: 940
In case we can't do it in annotations :
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/*
* Project
* @ORM\Entity(repositoryClass="ProjectRepository")
*/
class Project
{
use ORMBehaviors\Translatable\Translatable;
/*
* => @ Assert\Valid not working on $translations, since tranlastion already defined by ORMBehaviors trait
*/
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
//$metadata->addConstraint(new Assert\Valid());
$metadata->addPropertyConstraint('translations', new Assert\Valid());
}
Upvotes: 1
Reputation: 20223
It is done, the only thing I need to do is to set the validator on the top of the entity class:
/**
* Manuscript
*
* @IsDOI()
* @ORM\Table(name="manuscripts")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*
*/
class Manuscript
{...}
Upvotes: 0