[Validator] Fixed: Made it possible (again) to pass a class name to Validator::validatePropertyValue()
This commit is contained in:
parent
7e175ef8f3
commit
ef6f5f50c5
@ -904,6 +904,57 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertNull($violations[0]->getCode());
|
$this->assertNull($violations[0]->getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidatePropertyValueWithClassName()
|
||||||
|
{
|
||||||
|
$test = $this;
|
||||||
|
|
||||||
|
$callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
|
||||||
|
$propertyMetadatas = $test->metadata->getPropertyMetadata('firstName');
|
||||||
|
|
||||||
|
$test->assertSame($test::ENTITY_CLASS, $context->getClassName());
|
||||||
|
$test->assertSame('firstName', $context->getPropertyName());
|
||||||
|
$test->assertSame('', $context->getPropertyPath());
|
||||||
|
$test->assertSame('Group', $context->getGroup());
|
||||||
|
$test->assertSame($propertyMetadatas[0], $context->getMetadata());
|
||||||
|
$test->assertSame('Bernhard', $context->getRoot());
|
||||||
|
$test->assertSame('Bernhard', $context->getValue());
|
||||||
|
$test->assertSame('Bernhard', $value);
|
||||||
|
|
||||||
|
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||||
|
};
|
||||||
|
|
||||||
|
$callback2 = function ($value, ExecutionContextInterface $context) {
|
||||||
|
$context->addViolation('Other violation');
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->metadata->addPropertyConstraint('firstName', new Callback(array(
|
||||||
|
'callback' => $callback1,
|
||||||
|
'groups' => 'Group',
|
||||||
|
)));
|
||||||
|
$this->metadata->addPropertyConstraint('lastName', new Callback(array(
|
||||||
|
'callback' => $callback2,
|
||||||
|
'groups' => 'Group',
|
||||||
|
)));
|
||||||
|
|
||||||
|
$violations = $this->validatePropertyValue(
|
||||||
|
self::ENTITY_CLASS,
|
||||||
|
'firstName',
|
||||||
|
'Bernhard',
|
||||||
|
'Group'
|
||||||
|
);
|
||||||
|
|
||||||
|
/** @var ConstraintViolationInterface[] $violations */
|
||||||
|
$this->assertCount(1, $violations);
|
||||||
|
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||||
|
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||||
|
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||||
|
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||||
|
$this->assertSame('Bernhard', $violations[0]->getRoot());
|
||||||
|
$this->assertSame('Bernhard', $violations[0]->getInvalidValue());
|
||||||
|
$this->assertNull($violations[0]->getMessagePluralization());
|
||||||
|
$this->assertNull($violations[0]->getCode());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cannot be UnsupportedMetadataException for BC with Symfony < 2.5.
|
* Cannot be UnsupportedMetadataException for BC with Symfony < 2.5.
|
||||||
*
|
*
|
||||||
|
@ -142,7 +142,7 @@ class Validator implements ValidatorInterface, Mapping\Factory\MetadataFactoryIn
|
|||||||
*/
|
*/
|
||||||
public function validatePropertyValue($containingValue, $property, $value, $groups = null)
|
public function validatePropertyValue($containingValue, $property, $value, $groups = null)
|
||||||
{
|
{
|
||||||
$visitor = $this->createVisitor($containingValue);
|
$visitor = $this->createVisitor(is_object($containingValue) ? $containingValue : $value);
|
||||||
$metadata = $this->metadataFactory->getMetadataFor($containingValue);
|
$metadata = $this->metadataFactory->getMetadataFor($containingValue);
|
||||||
|
|
||||||
if (!$metadata instanceof PropertyMetadataContainerInterface) {
|
if (!$metadata instanceof PropertyMetadataContainerInterface) {
|
||||||
@ -153,13 +153,17 @@ class Validator implements ValidatorInterface, Mapping\Factory\MetadataFactoryIn
|
|||||||
throw new ValidatorException(sprintf('The metadata for '.$valueAsString.' does not support properties.'));
|
throw new ValidatorException(sprintf('The metadata for '.$valueAsString.' does not support properties.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If $containingValue is passed as class name, take $value as root
|
||||||
|
// and start the traversal with an empty property path
|
||||||
|
$propertyPath = is_object($containingValue) ? $property : '';
|
||||||
|
|
||||||
foreach ($this->resolveGroups($groups) as $group) {
|
foreach ($this->resolveGroups($groups) as $group) {
|
||||||
if (!$metadata->hasPropertyMetadata($property)) {
|
if (!$metadata->hasPropertyMetadata($property)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
|
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
|
||||||
$propMeta->accept($visitor, $value, $group, $property);
|
$propMeta->accept($visitor, $value, $group, $propertyPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,16 +68,16 @@ interface ContextualValidatorInterface
|
|||||||
* Validates a value against the constraints specified for an object's
|
* Validates a value against the constraints specified for an object's
|
||||||
* property.
|
* property.
|
||||||
*
|
*
|
||||||
* @param object $object The object
|
* @param object|string $objectOrClass The object or its class name
|
||||||
* @param string $propertyName The name of the property
|
* @param string $propertyName The name of the property
|
||||||
* @param mixed $value The value to validate against the
|
* @param mixed $value The value to validate against the
|
||||||
* property's constraints
|
* property's constraints
|
||||||
* @param array|null $groups The validation groups to validate. If
|
* @param array|null $groups The validation groups to validate. If
|
||||||
* none is given, "Default" is assumed
|
* none is given, "Default" is assumed
|
||||||
*
|
*
|
||||||
* @return ContextualValidatorInterface This validator
|
* @return ContextualValidatorInterface This validator
|
||||||
*/
|
*/
|
||||||
public function validatePropertyValue($object, $propertyName, $value, $groups = null);
|
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the violations that have been generated so far in the context
|
* Returns the violations that have been generated so far in the context
|
||||||
|
@ -194,6 +194,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
$propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
$propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
||||||
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
||||||
$cacheKey = spl_object_hash($object);
|
$cacheKey = spl_object_hash($object);
|
||||||
|
$propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
|
||||||
|
|
||||||
$previousValue = $this->context->getValue();
|
$previousValue = $this->context->getValue();
|
||||||
$previousObject = $this->context->getObject();
|
$previousObject = $this->context->getObject();
|
||||||
@ -209,7 +210,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
$object,
|
$object,
|
||||||
$cacheKey.':'.$propertyName,
|
$cacheKey.':'.$propertyName,
|
||||||
$propertyMetadata,
|
$propertyMetadata,
|
||||||
PropertyPath::append($this->defaultPropertyPath, $propertyName),
|
$propertyPath,
|
||||||
$groups,
|
$groups,
|
||||||
null,
|
null,
|
||||||
TraversalStrategy::IMPLICIT,
|
TraversalStrategy::IMPLICIT,
|
||||||
@ -226,9 +227,9 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function validatePropertyValue($object, $propertyName, $value, $groups = null)
|
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
|
||||||
{
|
{
|
||||||
$classMetadata = $this->metadataFactory->getMetadataFor($object);
|
$classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass);
|
||||||
|
|
||||||
if (!$classMetadata instanceof ClassMetadataInterface) {
|
if (!$classMetadata instanceof ClassMetadataInterface) {
|
||||||
// Cannot be UnsupportedMetadataException because of BC with
|
// Cannot be UnsupportedMetadataException because of BC with
|
||||||
@ -243,7 +244,17 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
|
|
||||||
$propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
$propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
|
||||||
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
||||||
$cacheKey = spl_object_hash($object);
|
|
||||||
|
if (is_object($objectOrClass)) {
|
||||||
|
$object = $objectOrClass;
|
||||||
|
$cacheKey = spl_object_hash($objectOrClass);
|
||||||
|
$propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
|
||||||
|
} else {
|
||||||
|
// $objectOrClass contains a class name
|
||||||
|
$object = null;
|
||||||
|
$cacheKey = null;
|
||||||
|
$propertyPath = $this->defaultPropertyPath;
|
||||||
|
}
|
||||||
|
|
||||||
$previousValue = $this->context->getValue();
|
$previousValue = $this->context->getValue();
|
||||||
$previousObject = $this->context->getObject();
|
$previousObject = $this->context->getObject();
|
||||||
@ -257,7 +268,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
$object,
|
$object,
|
||||||
$cacheKey.':'.$propertyName,
|
$cacheKey.':'.$propertyName,
|
||||||
$propertyMetadata,
|
$propertyMetadata,
|
||||||
PropertyPath::append($this->defaultPropertyPath, $propertyName),
|
$propertyPath,
|
||||||
$groups,
|
$groups,
|
||||||
null,
|
null,
|
||||||
TraversalStrategy::IMPLICIT,
|
TraversalStrategy::IMPLICIT,
|
||||||
|
@ -130,10 +130,11 @@ class RecursiveValidator implements ValidatorInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function validatePropertyValue($object, $propertyName, $value, $groups = null)
|
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
|
||||||
{
|
{
|
||||||
return $this->startContext($object)
|
// If a class name is passed, take $value as root
|
||||||
->validatePropertyValue($object, $propertyName, $value, $groups)
|
return $this->startContext(is_object($objectOrClass) ? $objectOrClass : $value)
|
||||||
|
->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
|
||||||
->getViolations();
|
->getViolations();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,18 +62,18 @@ interface ValidatorInterface extends MetadataFactoryInterface
|
|||||||
* Validates a value against the constraints specified for an object's
|
* Validates a value against the constraints specified for an object's
|
||||||
* property.
|
* property.
|
||||||
*
|
*
|
||||||
* @param object $object The object
|
* @param object|string $objectOrClass The object or its class name
|
||||||
* @param string $propertyName The name of the property
|
* @param string $propertyName The name of the property
|
||||||
* @param mixed $value The value to validate against the
|
* @param mixed $value The value to validate against the
|
||||||
* property's constraints
|
* property's constraints
|
||||||
* @param array|null $groups The validation groups to validate. If
|
* @param array|null $groups The validation groups to validate. If
|
||||||
* none is given, "Default" is assumed
|
* none is given, "Default" is assumed
|
||||||
*
|
*
|
||||||
* @return ConstraintViolationListInterface A list of constraint violations.
|
* @return ConstraintViolationListInterface A list of constraint violations.
|
||||||
* If the list is empty, validation
|
* If the list is empty, validation
|
||||||
* succeeded
|
* succeeded
|
||||||
*/
|
*/
|
||||||
public function validatePropertyValue($object, $propertyName, $value, $groups = null);
|
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a new validation context and returns a validator for that context.
|
* Starts a new validation context and returns a validator for that context.
|
||||||
|
@ -68,7 +68,7 @@ interface ValidatorInterface
|
|||||||
* The accepted values depend on the {@link MetadataFactoryInterface}
|
* The accepted values depend on the {@link MetadataFactoryInterface}
|
||||||
* implementation.
|
* implementation.
|
||||||
*
|
*
|
||||||
* @param string $containingValue The value containing the property.
|
* @param mixed $containingValue The value containing the property.
|
||||||
* @param string $property The name of the property to validate
|
* @param string $property The name of the property to validate
|
||||||
* @param string $value The value to validate against the
|
* @param string $value The value to validate against the
|
||||||
* constraints of the property.
|
* constraints of the property.
|
||||||
|
Reference in New Issue
Block a user