Skip to content

Instantly share code, notes, and snippets.

@tentacode
Created April 16, 2014 08:39
Show Gist options
  • Save tentacode/10833823 to your computer and use it in GitHub Desktop.
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
<?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