Symfony2 get validation constraints on an entity
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("Namespace\JobBundle\Entity\Job");
and $metadata
should have the data you are looking for
Symfony 2.3 and above
$metadata = $this->container ->get('validator') ->getMetadataFor("Namespace\JobBundle\Entity\Job");
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.