You are here

public function TextWidget::validate in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/field_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_example\Plugin\Field\FieldWidget\TextWidget::validate()

Validate the color text field.

File

field_example/src/Plugin/Field/FieldWidget/TextWidget.php, line 44

Class

TextWidget
Plugin implementation of the 'field_example_text' widget.

Namespace

Drupal\field_example\Plugin\Field\FieldWidget

Code

public function validate($element, FormStateInterface $form_state) {
  $value = $element['#value'];
  if (strlen($value) === 0) {
    $form_state
      ->setValueForElement($element, '');
    return;
  }
  if (!Color::validateHex($value)) {
    $form_state
      ->setError($element, $this
      ->t('Color must be a 3- or 6-digit hexadecimal value, suitable for CSS.'));
  }
}