You are here

public function Iso8601StringConstraintValidator::validate in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Validation/Constraint/Iso8601StringConstraintValidator.php \Drupal\duration_field\Plugin\Validation\Constraint\Iso8601StringConstraintValidator::validate()

File

src/Plugin/Validation/Constraint/Iso8601StringConstraintValidator.php, line 15

Class

Iso8601StringConstraintValidator
Validates the iso_8601_string constraint.

Namespace

Drupal\duration_field\Plugin\Validation\Constraint

Code

public function validate($items, Constraint $constraint) {
  if (is_array($items)) {
    foreach ($items as $item) {
      if (!$this
        ->isIso8601String($item)) {

        // The value is not a valid ISO 8601 duration string, so a violation,
        // aka error, is applied.
        $this->context
          ->addViolation($constraint->notIso8601, [
          '%value' => (string) $item,
        ]);
      }
    }
  }
  else {
    if (!$this
      ->isIso8601String($items)) {
      $this->context
        ->addViolation($constraint->notIso8601, [
        '%value' => (string) $items,
      ]);
    }
  }
}