Created
April 16, 2014 08:39
-
-
Save tentacode/10833823 to your computer and use it in GitHub Desktop.
Know if a property is required for current validation groups using Validator metadatas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isRequired($object, $property, $validationGroups = ['Default']) | |
{ | |
$classMetadata = $this->validator->getMetadataFor($object); | |
$propertyMetadatas = $classMetadata->getPropertyMetadata($property); | |
foreach ($propertyMetadatas as $propertyMetadata) { | |
$constraintsByGroups = $propertyMetadata->constraintsByGroup; | |
foreach ($constraintsByGroups as $group => $constraints) { | |
if (!in_array($group, $validationGroups)) { | |
continue; | |
} | |
foreach ($constraints as $constraint) { | |
if ($constraint instanceof NotBlank) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment