You are here

protected function Step::getRules in Clientside Validation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/CvValidator/Step.php \Drupal\clientside_validation\Plugin\CvValidator\Step::getRules()
  2. 3.0.x src/Plugin/CvValidator/Step.php \Drupal\clientside_validation\Plugin\CvValidator\Step::getRules()
  3. 2.0.x src/Plugin/CvValidator/Step.php \Drupal\clientside_validation\Plugin\CvValidator\Step::getRules()

Get the validation rules for this form element.

Return value

array An array with following keys:

  • rules: An array with the rulename as key and the rule arguments as value.
  • messages: An array with the rulename as key and the message for this rule as argument.

Overrides CvValidatorBase::getRules

File

src/Plugin/CvValidator/Step.php, line 24

Class

Step
Provides a 'step' validator.

Namespace

Drupal\clientside_validation\Plugin\CvValidator

Code

protected function getRules($element, FormStateInterface $form_state) {
  $step = $this
    ->getAttributeValue($element, 'step');
  if ($step !== 'any') {
    if (isset($element['#step_error'])) {
      $message = $element['#step_error'];
    }
    elseif ($min = $this
      ->getAttributeValue($element, 'min')) {
      $message = $this
        ->t('The value in @title has to be greater than @min by steps of @step.', [
        '@title' => $this
          ->getElementTitle($element),
        '@step' => $step,
        '@min' => $min,
      ]);
    }
    else {
      $message = $this
        ->t('The value in @title has to be divisible by @step.', [
        '@title' => $this
          ->getElementTitle($element),
        '@step' => $step,
      ]);
    }
    return [
      'messages' => [
        'step' => $message,
      ],
    ];
  }
}