symfony2 获取实体上的所有验证约束(yml、xml、注释) [英] symfony2 get all validation constraints on an entity (yml, xml, annotations)

查看:25
本文介绍了symfony2 获取实体上的所有验证约束(yml、xml、注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取实体上的所有验证约束并将这些约束转换为 Jquery 验证规则,现在我能够获得注释定义的约束(感谢:Symfony2 获取实体的验证约束),但我在获取 xml 和 yml 时遇到了一些麻烦.

Im trying to get all validation constraints on an entity and translate theses constraints to Jquery validation rules, right now im able to get annotation defined constraints (thanks to : Symfony2 get validation constraints on an entity), but im having some trouble getting xml and yml ones.

$xml_file_loader = new XmlFileLoader("path_to_my_project/vendor/friendsofsymfony/user-bundle\FOS\UserBundle\Resources\config\validation.xml");

使用类似的代码意味着我需要事先知道 xml/yml 文件所在的位置,我正在尝试以某种方式编写一个可以自动执行此操作的通用代码.

Using a similar code means that i need to know beforehand where the xml/yml file is located, i m trying to write somehow a generic code that can do this automatically.

没有办法一次性获得所有约束吗?如果不是,我怎么知道 xml/yml 文件的位置,以及在继承的情况下我需要检查父约束...这可行吗?

Isn't there a way to get all constraints at once? if not how can i know the location of xml/yml files, and also in cases of inheritance i need to check for parent constraints... Is this doable?

推荐答案

private function getValidations()
    {
        $validations=[];
        $validator=$this->get("validator");
        $metadata=$validator->getMetadataFor(new your_entity());
        $constrainedProperties=$metadata->getConstrainedProperties();
        foreach($constrainedProperties as $constrainedProperty)
        {
            $propertyMetadata=$metadata->getPropertyMetadata($constrainedProperty);
            $constraints=$propertyMetadata[0]->constraints;
            $outputConstraintsCollection=[];
            foreach($constraints as $constraint)
            {
                $class = new \ReflectionObject($constraint);
                $constraintName=$class->getShortName();
                $constraintParameter=null;
                switch ($constraintName) 
                {
                    case "NotBlank":
                        $param="notBlank";
                        break;
                    case "Type":
                        $param=$constraint->type;
                        break;
                    case "Length":
                        $param=$constraint->max;
                        break;
                }
                $outputConstraintsCollection[$constraintName]=$param;
            }
            $validations[$constrainedProperty]=$outputConstraintsCollection;
        }
        return $validations;
    }

退货:

array(13) (
      [property1] => array(4) (
        [NotBlank] => (string) notBlank
        [NotNull] => (string) notBlank
        [Type] => (string) string
        [Length] => (int) 11
      )
      [property2] => array(4) (
        [NotBlank] => (string) notBlank
        [NotNull] => (string) notBlank
        [Type] => (string) string
        [Length] => (int) 40
      )
      ..........
)

返回的数组可以配置或用于定义客户端验证规则,具体取决于您使用的客户端验证库/代码

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

对象 $metadata 现在包含有关与您的特定实体相关的验证的所有元数据.

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

这篇关于symfony2 获取实体上的所有验证约束(yml、xml、注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆