You are here

public function Validator::validateValue in Plug 7

Validates a value against a constraint or a list of constraints.

@api

Parameters

mixed $value The value to validate.:

Constraint|Constraint[] $constraints The constraint(s) to validate against.:

array|null $groups The validation groups to validate.:

Return value

ConstraintViolationListInterface A list of constraint violations. If the list is empty, validation succeeded.

Overrides ValidatorInterface::validateValue

Deprecated

Renamed to {@link Validator\ValidatorInterface::validate()} in Symfony 2.5. Will be removed in Symfony 3.0.

File

lib/Symfony/validator/Symfony/Component/Validator/Validator.php, line 175

Class

Validator
Default implementation of {@link ValidatorInterface}.

Namespace

Symfony\Component\Validator

Code

public function validateValue($value, $constraints, $groups = null) {
  $context = new ExecutionContext($this
    ->createVisitor($value), $this->translator, $this->translationDomain);
  $constraints = is_array($constraints) ? $constraints : array(
    $constraints,
  );
  foreach ($constraints as $constraint) {
    if ($constraint instanceof Valid) {

      // Why can't the Valid constraint be executed directly?
      //
      // It cannot be executed like regular other constraints, because regular
      // constraints are only executed *if they belong to the validated group*.
      // The Valid constraint, on the other hand, is always executed and propagates
      // the group to the cascaded object. The propagated group depends on
      //
      //  * Whether a group sequence is currently being executed. Then the default
      //    group is propagated.
      //
      //  * Otherwise the validated group is propagated.
      throw new ValidatorException(sprintf('The constraint %s cannot be validated. Use the method validate() instead.', get_class($constraint)));
    }
    $context
      ->validateValue($value, $constraint, '', $groups);
  }
  return $context
    ->getViolations();
}