You are here

public function ColorForm::colorElementValidate in Color API 8

Custom element validation handler for the 'color' property.

Creates a TypedDataAPI colorapi_color object to work with, setting it as the value for the element.

File

src/Form/ColorForm.php, line 142

Class

ColorForm
Form handler for the Color add and edit forms.

Namespace

Drupal\colorapi\Form

Code

public function colorElementValidate(array &$element, FormStateInterface $form_state) {
  $hexadecimal_color = $form_state
    ->getValue('color');
  if (!$this->colorapiService
    ->isValidHexadecimalColorString($hexadecimal_color)) {
    $form_state
      ->setError($element, $this
      ->t('%value is not a valid hexadecimal color string. Hexadecimal color strings are in the format #XXXXXX where X is a hexadecimal character (0-9, a-f).', [
      '%value' => $hexadecimal_color,
    ]));
  }
}