You are here

protected function MaxLength::getRules in Clientside Validation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/CvValidator/MaxLength.php \Drupal\clientside_validation\Plugin\CvValidator\MaxLength::getRules()
  2. 3.0.x src/Plugin/CvValidator/MaxLength.php \Drupal\clientside_validation\Plugin\CvValidator\MaxLength::getRules()
  3. 2.0.x src/Plugin/CvValidator/MaxLength.php \Drupal\clientside_validation\Plugin\CvValidator\MaxLength::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/MaxLength.php, line 24

Class

MaxLength
Provides a 'maxlength' validator.

Namespace

Drupal\clientside_validation\Plugin\CvValidator

Code

protected function getRules($element, FormStateInterface $form_state) {

  // Drupal already adds the maxlength attribute, so we don't need to set the
  // maxlength rule.
  if (isset($element['#maxlength_error'])) {
    $message = $element['#maxlength_error'];
  }
  elseif (isset($element['#type']) && $element['#type'] == 'select') {
    $message = $this
      ->t('@title field can only have a maximum of @max values.', [
      '@title' => $this
        ->getElementTitle($element),
      '@max' => $this
        ->getAttributeValue($element, 'maxlength'),
    ]);
  }
  else {
    $message = $this
      ->t('@title field has a maximum length of @max.', [
      '@title' => $this
        ->getElementTitle($element),
      '@max' => $this
        ->getAttributeValue($element, 'maxlength'),
    ]);
  }
  return [
    'messages' => [
      'maxlength' => $message,
    ],
  ];
}