You are here

public function GranularityStringConstraintValidator::validate in Duration Field 8.2

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

File

src/Plugin/Validation/Constraint/GranularityStringConstraintValidator.php, line 16

Class

GranularityStringConstraintValidator
Validates the granularity_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
        ->isGranularityString($item)) {

        // The value is not an integer, so a violation, aka error, is applied.
        // The type of violation applied comes from the constraint description
        // in step 1.
        $this->context
          ->addViolation($constraint->notValidGranularityString, [
          '%value' => (string) $item,
        ]);
      }
    }
  }
  else {
    if (!$this
      ->isGranularityString($items)) {
      $this->context
        ->addViolation($constraint->notValidGranularityString, [
        '%value' => (string) $items,
      ]);
    }
  }
}