0
votes

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.

2
and what is your question and what do you not understand?Wouter J
My question is: How to access some other entity values from the validator class. The problem is that I do not know where to set the validation in order to be able to get access to other entity valuesMilos Cuculovic

2 Answers

1
votes

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());
}
0
votes

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
{...}