Skip to content

Commit

Permalink
[Validator] Add check for existing metadata on property
Browse files Browse the repository at this point in the history
  • Loading branch information
dlsniper authored and fabpot committed Feb 11, 2013
1 parent 60cf0aa commit 4cbdbcb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Expand Up @@ -301,6 +301,14 @@ public function getMemberMetadatas($property)
return $this->members[$property];
}

/**
* {@inheritdoc}
*/
public function hasPropertyMetadata($property)
{
return array_key_exists($property, $this->members);
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -18,6 +18,15 @@
*/
interface PropertyMetadataContainerInterface
{
/**
* Check if there's any metadata attached to the given named property.
*
* @param string $property The property name.
*
* @return Boolean
*/
public function hasPropertyMetadata($property);

/**
* Returns all metadata instances for the given named property.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Tests/ValidatorTest.php
Expand Up @@ -187,6 +187,10 @@ public function testValidateProperty()
$result = $this->validator->validateProperty($entity, 'firstName');

$this->assertCount(1, $result);

$result = $this->validator->validateProperty($entity, 'lastName');

$this->assertCount(0, $result);
}

public function testValidatePropertyValue()
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Validator.php
Expand Up @@ -112,6 +112,10 @@ public function validateProperty($containingValue, $property, $groups = null)
}

foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}

foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
}
Expand Down Expand Up @@ -139,6 +143,10 @@ public function validatePropertyValue($containingValue, $property, $value, $grou
}

foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}

foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $value, $group, $property);
}
Expand Down

0 comments on commit 4cbdbcb

Please sign in to comment.