Validate that the element is not longer than the max length.
array $element: The input element to validate.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
public function validateMaxLength(array $element, FormStateInterface &$form_state) {
if (isset($element['#max_length']) && $element['#max_length'] < mb_strlen($element['#value'])) {
$form_state
->setError($element, $this
->t('The field has @size characters while the limit is @limit.', [
'@size' => mb_strlen($element['#value']),
'@limit' => $element['#max_length'],
]));
}
}