13

Im working on a method to get all validation constraints of an entity (what i am trying to achieve is to return this data in JSON and apply the same constraints on client side using JQuery Validation Plugin), however im having some trouble getting the constraints, Here is my current code:

    $metadata = new \Symfony\Component\Validator\Mapping\ClassMetadata("Namespace\JobBundle\Entity\Job");
    $annotationloader = new AnnotationLoader(new AnnotationReader());
    $annotationloader->loadClassMetadata($metadata);

what i get in $metadata is an empty array for the constraints attribute, the rest ($properties and $members have only the error messages... but not the actual constraints (eg : required, integer...)).

What im a doing wrong?

2
  • Did you try $metadata = $this->container->get('validator')->getMetadataFactory()->getClassMetadata("Namespace\JobBundle\Entity\Job");? Commented Mar 18, 2013 at 23:37
  • just noticed that the $properties attribute actually contains a constraints array, your suggestion also gives the same result, so if you make answer i'd gladly accept it. Commented Mar 19, 2013 at 11:58

2 Answers 2

18

I would probably use the validator service instead of instantiating a new class metadata. You never know if some classes are initialized through the service.

$metadata = $this->container
                 ->get('validator')
                 ->getMetadataFactory()
                 ->getClassMetadata("Name‌​space\JobBundle\Entity\Job");

and $metadata should have the data you are looking for

Symfony 2.3 and above

$metadata = $this->container
                 ->get('validator')
                 ->getMetadataFor("Name‌​space\JobBundle\Entity\Job");
Sign up to request clarification or add additional context in comments.

2 Comments

+1, btw from Symfony 2.3 getClassMetadata is deprecated and getMetadataFor is recommended.
Works with Symfony 4.0
7
private function getValidations()
    {
        $validator=$this->get("validator");
        $metadata=$validator->getMetadataFor(new yourentity());
        $constrainedProperties=$metadata->getConstrainedProperties();
        foreach($constrainedProperties as $constrainedProperty)
        {
            $propertyMetadata=$metadata->getPropertyMetadata($constrainedProperty);
            $constraints=$propertyMetadata[0]->constraints;
            foreach($constraints as $constraint)
            {
                //here you can use $constraint to get the constraint, messages etc that apply to a particular property of your entity
            }
        }
    }

$validator=$this->get("validator");
$metadata=$validator->getMetadataFor(new yourentity());

The object $metadata now contains all the metadata about validations that concerns your specific entity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.