symfony2 get all validation constraints on an entity (yml, xml, annotations)

断了今生、忘了曾经 提交于 2019-11-30 17:22:15

问题


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");

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.

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?


回答1:


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;
    }

Returns:

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
      )
      ..........
)

The returned array can be configured or used to define client side validation rules depending on the client-side validation library/code that you are using

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

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



来源:https://stackoverflow.com/questions/15573935/symfony2-get-all-validation-constraints-on-an-entity-yml-xml-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!