2016-03-25 14 views
2

Ich möchte Rückruf-Validator und/oder Getter aber alle ausgelassen in der einfachen Einheit:Symfony 3/Doctrine 2: Warum mein Validator ausgelassen wurde?

AppBundle/Entity/page.php:

public static function loadValidatorMetadata(ClassMetadata $metadata) 
{  
    $metadata->addConstraint(new Assert\Callback('validate')); 

    $metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
     'message' => 'The password cannot match your first name', 
    ))); 

} 

public static function validate($object, ExecutionContextInterface $context) 
{ 
    die('validate'); 
} 

public function isPasswordLegal() 
{ 
    die('isPasswordLegal'); 
} 

loadValidatorMetadata ausgeführt, aber validate und isPasswordLegal nicht. Warum? Validierung ist aktiviert.

config.yml:

validation:  { enabled: true, enable_annotations: true } 

I validation.yml versucht:

AppBundle\Entity\Page: 
constraints: 
    - Callback: [validate] 

Danke.

+1

wie auslösen Sie Validierung? Formular einreichen? bestätigen? die Entität speichern? –

+0

Formularübergabe mit Doctrine-Entität – Vandorr

Antwort

0

fand ich mich einen funktionierenden Code:

public static function loadValidatorMetadata(ClassMetadata $metadata) 
{ 
    $callback = function ($object, ExecutionContextInterface $context) { 
     $context->buildViolation($object->getTemplate().' sounds totally fake!') 
     //->atPath('template') //Global or Field error 
     ->addViolation(); 
    }; 

    $metadata->addConstraint(new Assert\Callback($callback)); 
} 

dies in der Einheit setzen: Yourbundle/Entity/EntityName.php