diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php index 8ce479b3dd32..6ea9f2f6d957 100644 --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php @@ -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} */ diff --git a/src/Symfony/Component/Validator/PropertyMetadataContainerInterface.php b/src/Symfony/Component/Validator/PropertyMetadataContainerInterface.php index 33db49e4e1c6..20bafb2950fd 100644 --- a/src/Symfony/Component/Validator/PropertyMetadataContainerInterface.php +++ b/src/Symfony/Component/Validator/PropertyMetadataContainerInterface.php @@ -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. * diff --git a/src/Symfony/Component/Validator/Tests/ValidatorTest.php b/src/Symfony/Component/Validator/Tests/ValidatorTest.php index 502671fd8e31..dbfa4f298d31 100644 --- a/src/Symfony/Component/Validator/Tests/ValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/ValidatorTest.php @@ -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() diff --git a/src/Symfony/Component/Validator/Validator.php b/src/Symfony/Component/Validator/Validator.php index a700692d8d02..8221d6026695 100644 --- a/src/Symfony/Component/Validator/Validator.php +++ b/src/Symfony/Component/Validator/Validator.php @@ -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); } @@ -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); }