You are here

public static function WebformElementBase::validateMinlength in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::validateMinlength()

Form API callback. Validate element #minlength value.

File

src/Plugin/WebformElementBase.php, line 1936

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

public static function validateMinlength(&$element, FormStateInterface &$form_state) {
  if (!isset($element['#minlength'])) {
    return;
  }
  if (!empty($element['#value']) && mb_strlen($element['#value']) < $element['#minlength']) {
    $t_args = [
      '%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'],
      '%min' => $element['#minlength'],
      '%length' => mb_strlen($element['#value']),
    ];
    $form_state
      ->setError($element, t('%name cannot be less than %min characters but is currently %length characters long.', $t_args));
  }
}