Same name and namespace in other branches
  1. 7.x-2.x color_field.field.inc \color_field_field_validate()

Implements hook_field_validate().

We want to verify that the items only contain RGB hex values like this: #RRGGBB. If the item validates, we do nothing. If it doesn't validate, we add our own error notification to the $errors parameter.

See also

color_field_widget_error()

File

./color_field.module, line 229

Code

function color_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
    foreach ($items as $delta => $item) {
        if (!empty($item['rgb'])) {
            if (!preg_match('@^#[0-9a-fA-F]{6}$@', $item['rgb'])) {
                $errors[$field['field_name']][$langcode][$delta][] = array(
                    'error' => 'color_field_invalid',
                    'message' => t('Color must be in the hexadecimal format #abcdef.'),
                );
            }
        }
    }
}