[Validator] Changed validateValue() to validate() in the new API
The validation of values against constraints should be a first-class citizen in the API. For this reason, validate() now takes a value and a constraint or a list of constraints. This method should be used for the most basic use cases. If users want to annotate objects with constraints (this is optional, advanced functionality), they can use the more expressive validateObject() method now. For traversing arrays or Traversables, a new method validateCollection() is now available in the API.
This commit is contained in:
parent
ee1adadbfb
commit
718601c6c3
|
@ -112,7 +112,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
|
|||
->getValidator()
|
||||
->inContext($this)
|
||||
->atPath($subPath)
|
||||
->validateValue($value, $constraint, $groups)
|
||||
->validate($value, $constraint, $groups)
|
||||
;
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
|
|||
->getValidator()
|
||||
->inContext($this)
|
||||
->atPath($subPath)
|
||||
->validateValue($value, $constraints, $groups)
|
||||
->validate($value, $constraints, $groups)
|
||||
;
|
||||
}
|
||||
|
||||
|
@ -147,7 +147,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
|
|||
->getValidator()
|
||||
->inContext($this)
|
||||
->atPath($subPath)
|
||||
->validateValue($value, $constraints, $groups)
|
||||
->validate($value, $constraints, $groups)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,23 @@ class ContextualValidator extends AbstractValidator implements ContextualValidat
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validate($value, $constraints, $groups = null)
|
||||
{
|
||||
$this->traverseValue($value, $constraints, $groups);
|
||||
|
||||
return $this->context->getViolations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a value.
|
||||
*
|
||||
|
@ -118,21 +135,4 @@ class ContextualValidator extends AbstractValidator implements ContextualValidat
|
|||
|
||||
return $this->context->getViolations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validateValue($value, $constraints, $groups = null)
|
||||
{
|
||||
$this->traverseValue($value, $constraints, $groups);
|
||||
|
||||
return $this->context->getViolations();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Symfony\Component\Validator\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\Traverse;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
|
||||
|
@ -23,6 +24,11 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface
|
|||
{
|
||||
public function validate($value, $groups = null, $traverse = false, $deep = false)
|
||||
{
|
||||
// Use new signature if constraints are given in the second argument
|
||||
if (func_num_args() <= 3 && ($groups instanceof Constraint || (is_array($groups) && current($groups) instanceof Constraint))) {
|
||||
return parent::validate($value, $groups, $traverse);
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$constraint = new Traverse(array(
|
||||
'traverse' => true,
|
||||
|
@ -44,6 +50,11 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface
|
|||
return $this->validateObject($value, $groups);
|
||||
}
|
||||
|
||||
public function validateValue($value, $constraints, $groups = null)
|
||||
{
|
||||
return parent::validate($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
public function getMetadataFactory()
|
||||
{
|
||||
return $this->metadataFactory;
|
||||
|
|
|
@ -34,6 +34,15 @@ class Validator extends AbstractValidator
|
|||
$this->contextManager = $contextManager;
|
||||
}
|
||||
|
||||
public function validate($value, $constraints, $groups = null)
|
||||
{
|
||||
$this->contextManager->startContext($value);
|
||||
|
||||
$this->traverseValue($value, $constraints, $groups);
|
||||
|
||||
return $this->contextManager->stopContext()->getViolations();
|
||||
}
|
||||
|
||||
public function validateObject($object, $groups = null)
|
||||
{
|
||||
$this->contextManager->startContext($object);
|
||||
|
@ -74,14 +83,4 @@ class Validator extends AbstractValidator
|
|||
|
||||
return $this->contextManager->stopContext()->getViolations();
|
||||
}
|
||||
|
||||
public function validateValue($value, $constraints, $groups = null)
|
||||
{
|
||||
$this->contextManager->startContext($value);
|
||||
|
||||
$this->traverseValue($value, $constraints, $groups);
|
||||
|
||||
return $this->contextManager->stopContext()->getViolations();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,18 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|||
*/
|
||||
interface ValidatorInterface
|
||||
{
|
||||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validate($value, $constraints, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a value.
|
||||
*
|
||||
|
@ -69,18 +81,6 @@ interface ValidatorInterface
|
|||
*/
|
||||
public function validatePropertyValue($object, $propertyName, $value, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validateValue($value, $constraints, $groups = null);
|
||||
|
||||
/**
|
||||
* @param ExecutionContextInterface $context
|
||||
*
|
||||
|
|
Reference in New Issue