[Validator] Add check for existing metadata on property

This commit is contained in:
Florin Patan 2013-02-05 12:07:10 +02:00 committed by Fabien Potencier
parent 60cf0aa181
commit 4cbdbcb1f9
4 changed files with 29 additions and 0 deletions

View File

@ -301,6 +301,14 @@ class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassB
return $this->members[$property];
}
/**
* {@inheritdoc}
*/
public function hasPropertyMetadata($property)
{
return array_key_exists($property, $this->members);
}
/**
* {@inheritdoc}
*/

View File

@ -18,6 +18,15 @@ namespace Symfony\Component\Validator;
*/
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.
*

View File

@ -187,6 +187,10 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
$result = $this->validator->validateProperty($entity, 'firstName');
$this->assertCount(1, $result);
$result = $this->validator->validateProperty($entity, 'lastName');
$this->assertCount(0, $result);
}
public function testValidatePropertyValue()

View File

@ -112,6 +112,10 @@ class Validator implements ValidatorInterface
}
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 @@ class Validator implements ValidatorInterface
}
foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $value, $group, $property);
}