You are here

public function Calendar::validateHexColor in Calendar 8

Check to make sure the user has entered a valid 6 digit hex color.

Parameters

array $element: Element to validate.

\Drupal\Core\Form\FormStateInterface $form_state: State of the form.

File

src/Plugin/views/row/Calendar.php, line 319

Class

Calendar
Plugin which creates a view on the resulting object and formats it as a Calendar entity.

Namespace

Drupal\calendar\Plugin\views\row

Code

public function validateHexColor(array $element, FormStateInterface $form_state) {
  if (!$element['#required'] && empty($element['#value'])) {
    return;
  }
  if (!preg_match('/^#(?:(?:[a-f\\d]{3}){1,2})$/i', $element['#value'])) {
    $form_state
      ->setError($element, $this
      ->t("'@color' is not a valid hex color", [
      '@color' => $element['#value'],
    ]));
  }
  else {
    $form_state
      ->setValueForElement($element, $element['#value']);
  }
}