Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
and what is your question and what do you not understand? – Wouter J Jul 12 '13 at 17:49
    
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 values – Miloš Jul 15 '13 at 14:23

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());
}
share|improve this answer
up vote 0 down vote accepted

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
{...}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.