You are here

public function ExecutionContext::validate in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/validator/ExecutionContext.php \Symfony\Component\Validator\ExecutionContext::validate()
  2. 8 vendor/symfony/validator/Context/ExecutionContext.php \Symfony\Component\Validator\Context\ExecutionContext::validate()
  3. 8 core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php \Drupal\Core\TypedData\Validation\ExecutionContext::validate()
Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Context/ExecutionContext.php \Symfony\Component\Validator\Context\ExecutionContext::validate()

Validates the given value within the scope of the current validation.

The value may be any value recognized by the used metadata factory (see {@link MetadataFactoryInterface::getMetadata}), or an array or a traversable object of such values.

Usually you validate a value that is not the current node of the execution context. For this case, you can pass the {@link $subPath} argument which is appended to the current property path when a violation is created. For example, take the following object graph:

<pre> (Person)---($address: Address)---($phoneNumber: PhoneNumber) ^ </pre>

When the execution context stops at the <tt>Person</tt> instance, the property path is "address". When you validate the <tt>PhoneNumber</tt> instance now, pass "phoneNumber" as sub path to correct the property path to "address.phoneNumber":

<pre> $context->validate($address->phoneNumber, 'phoneNumber'); </pre>

Any violations generated during the validation will be added to the violation list that you can access with {@link getViolations}.

Parameters

mixed $value The value to validate.:

string $subPath The path to append to the context's property path.:

null|string|string[] $groups The groups to validate in. If you don't pass any: groups here, the current group of the context will be used.

bool $traverse Whether to traverse the value if it is an array: or an instance of <tt>\Traversable</tt>.

bool $deep Whether to traverse the value recursively if: it is a collection of collections.

Overrides ExecutionContextInterface::validate

Deprecated

since version 2.5, to be removed in 3.0. Use {@link Context\ExecutionContextInterface::getValidator()} instead.

File

vendor/symfony/validator/Context/ExecutionContext.php, line 346

Class

ExecutionContext
The context used and created by {@link ExecutionContextFactory}.

Namespace

Symfony\Component\Validator\Context

Code

public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false) {
  @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0. Use the ' . __CLASS__ . '::getValidator() method instead.', E_USER_DEPRECATED);
  if (is_array($value)) {

    // The $traverse flag is ignored for arrays
    $constraint = new Valid(array(
      'traverse' => true,
      'deep' => $deep,
    ));
    return $this
      ->getValidator()
      ->inContext($this)
      ->atPath($subPath)
      ->validate($value, $constraint, $groups);
  }
  if ($traverse && $value instanceof \Traversable) {
    $constraint = new Valid(array(
      'traverse' => true,
      'deep' => $deep,
    ));
    return $this
      ->getValidator()
      ->inContext($this)
      ->atPath($subPath)
      ->validate($value, $constraint, $groups);
  }
  return $this
    ->getValidator()
    ->inContext($this)
    ->atPath($subPath)
    ->validate($value, null, $groups);
}