You are here

class RegExValidationConstraintValidator 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
  2. 2.0.x src/Plugin/Validation/Constraint/RegExValidationConstraintValidator.php \Drupal\regex_field_validation\Plugin\Validation\Constraint\RegExValidationConstraintValidator

Implementing Custom RegEx Validator Class.

Hierarchy

Expanded class hierarchy of RegExValidationConstraintValidator

File

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

Namespace

Drupal\regex_field_validation\Plugin\Validation\Constraint
View source
class RegExValidationConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  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));
      }
    }
  }

}

Members