You are here

public function JQueryColorpickerService::validateColor in Jquery Colorpicker 8

Validates a hecidecimal color string.

The following rules are validated:

  • Length is six characters
  • Value is hexidecimal.

Parameters

string $color: The color string to be validated.

Return value

bool|\Drupal\Core\StringTranslation\TranslatableMarkup FALSE if there are no errors, or a TranslateabeMarkup object containing the error message if there are any errors.

Overrides JQueryColorpickerServiceInterface::validateColor

File

src/Service/JQueryColorpickerService.php, line 37

Class

JQueryColorpickerService
The jQuery Colorpicker service.

Namespace

Drupal\jquery_colorpicker\Service

Code

public function validateColor($color) {
  $error = FALSE;
  if (is_string($color) || is_int($color)) {
    if (strlen($color) != 6) {
      $error = $this
        ->t('Color values must be exactly six characters in length');
    }
    elseif (!preg_match('/^[0-9a-fA-F]{6}$/i', $color)) {
      $error = $this
        ->t("You entered an invalid value for the color. Colors must be hexadecimal, and can only contain the characters '0-9', 'a-f' and/or 'A-F'.");
    }
  }
  else {
    $error = $this
      ->t('Color must be a string or an integer');
  }
  return $error;
}