1 form.inc form_validate_color(&$element, &$form_state)

Form element validation handler for #type 'color'.

Related topics

File

core/includes/form.inc, line 4235
Functions for form and batch generation and processing.

Code

function form_validate_color(&$element, &$form_state) {
  $value = trim($element['#value']);

  // Default to black if no value is given.
  // @see http://www.w3.org/TR/html5/number-state.html#color-state
  if ($value === '') {
    form_set_value($element, '#000000', $form_state);
  }
  else {
    // Try to parse the value and normalize it.
    try {
      form_set_value($element, Color::rgbToHex(Color::hexToRgb($value)), $form_state);
    }
    catch (InvalidArgumentException $e) {
      form_error($element, t('%name must be a valid color.', array('%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'])));
    }
  }
}