You are here

public static function JQueryColorpicker::validateElement in Jquery Colorpicker 8

1 call to JQueryColorpicker::validateElement()
JQueryColorpickerTest::testValidateElementEmpty in tests/src/Unit/Element/JQueryColorpickerTest.php
@covers ::valueCallback

File

src/Element/JQueryColorpicker.php, line 50

Class

JQueryColorpicker
Provides a jQuery colorpicker form element.

Namespace

Drupal\jquery_colorpicker\Element

Code

public static function validateElement(&$element, FormStateInterface $form_state) {
  if (strlen($element['#value'])) {
    $jquery_colorpicker_service = \Drupal::service('jquery_colorpicker.service');
    $color = $jquery_colorpicker_service
      ->formatColor($element['#value']);
    if ($color != $element['#value']) {
      $form_state
        ->setValueForElement($element, $color);
    }
    $error = $jquery_colorpicker_service
      ->validateColor($element['#value']);
    if ($error) {
      $form_state
        ->setError($element, $error);
    }
  }
}