Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
symfony2获取实体上的所有验证约束(yml、xml、注释)_Symfony_Symfony Forms - Fatal编程技术网

symfony2获取实体上的所有验证约束(yml、xml、注释)

symfony2获取实体上的所有验证约束(yml、xml、注释),symfony,symfony-forms,Symfony,Symfony Forms,我试图获取实体上的所有验证约束,并将这些约束转换为Jquery验证规则,现在我能够获取注释定义的约束(感谢:),但在获取xml和yml约束时遇到了一些问题 $xml_file_loader = new XmlFileLoader("path_to_my_project/vendor/friendsofsymfony/user-bundle\FOS\UserBundle\Resources\config\validation.xml"); 使用类似的代码意味着我需要事先知道xml/yml文件位于

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

$xml_file_loader = new XmlFileLoader("path_to_my_project/vendor/friendsofsymfony/user-bundle\FOS\UserBundle\Resources\config\validation.xml");
使用类似的代码意味着我需要事先知道xml/yml文件位于何处,我正试图以某种方式编写一个可以自动完成这项工作的通用代码

难道没有一种方法可以一次获得所有约束吗?如果没有,我如何知道xml/yml文件的位置,并且在继承的情况下,我需要检查父约束。。。这可行吗

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
现在包含与特定实体相关的验证的所有元数据。

出于这个原因,您可以使用-。我已经尝试过了,它只会得到注释约束…我正在为此添加一个问题(我知道这是很久以前的事了)。我遵循了你的剧本,进展顺利。但是在我的constraint.yml中,我在消息中使用了{limit},当我使用脚本时,symfony不会解析这个关键字。你有办法吗?我试过了,它不在乎父约束。