You are here

public function RegExValidationConstraintValidator::validate in RegEx Field Validation 1.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Validation/Constraint/RegExValidationConstraintValidator.php \Drupal\regex_field_validation\Plugin\Validation\Constraint\RegExValidationConstraintValidator::validate()
  2. 2.0.x src/Plugin/Validation/Constraint/RegExValidationConstraintValidator.php \Drupal\regex_field_validation\Plugin\Validation\Constraint\RegExValidationConstraintValidator::validate()

File

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

Class

RegExValidationConstraintValidator
Implementing Custom RegEx Validator Class.

Namespace

Drupal\regex_field_validation\Plugin\Validation\Constraint

Code

public function validate($items, Constraint $constraint) {
  $field = $this->context
    ->getValue();
  $value = $field
    ->getValue();
  if (!empty($value)) {
    if (!preg_match_all($constraint->regex, $value[0]['value'])) {
      $this->context
        ->addViolation($this
        ->t($constraint->errorMessage));
    }
  }
}