You are here

public static function ScssColor::validateColor in SCSS Compiler 1.0.x

Validate this element's value on form submission.

Parameters

array $element: The element that should be validated.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form.

File

src/Element/ScssColor.php, line 117

Class

ScssColor
A form element to represent null-able Sass colors.

Namespace

Drupal\compiler_scss\Element

Code

public static function validateColor(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Extract the input value from the element.
  $input = self::getValue($element);

  // Represent the input as a typed data instance to facilitate validation.
  $definition = DataDefinition::create('compiler_scss_color');
  $color = \Drupal::typedDataManager()
    ->create($definition, $input);

  // Check if the input value failed validation.
  if ($color
    ->validate()
    ->count() > 0) {
    $error = $element['#title'] ? t('%name must be a valid color.', [
      '%name' => $element['#title'],
    ]) : t('This field must be a valid color.');
    $form_state
      ->setError($element, $error);
  }
  else {
    $form_state
      ->setValueForElement($element, $input ?: NULL);
  }
}