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:
我正在研究获取实体的所有验证约束的方法(我想要实现的是使用JQuery Validation Plugin在JSON中返回此数据并在客户端应用相同的约束),但是在获取约束时遇到一些麻烦,这是我目前的代码:
$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...)).
我在$ metadata中获得的是约束属性的空数组,其余的($ properties和$ members只有错误消息......但不是实际约束(例如:required,integer ...))。
What im a doing wrong?
我做错了什么?
2 个解决方案
#1
16
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
和$ metadata应该包含您要查找的数据
Symfony 2.3 and above
Symfony 2.3及以上
$metadata = $this->container
->get('validator')
->getMetadataFor("Namespace\JobBundle\Entity\Job");
#2
6
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.
$验证= $这个 - > GET( “验证”); $ metadata = $ validator-> getMetadataFor(new yourentity());对象$ metadata现在包含有关您的特定实体的验证的所有元数据。
#1
16
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
和$ metadata应该包含您要查找的数据
Symfony 2.3 and above
Symfony 2.3及以上
$metadata = $this->container
->get('validator')
->getMetadataFor("Namespace\JobBundle\Entity\Job");
#2
6
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.
$验证= $这个 - > GET( “验证”); $ metadata = $ validator-> getMetadataFor(new yourentity());对象$ metadata现在包含有关您的特定实体的验证的所有元数据。