Same name and namespace in other branches
  1. 7.x-1.x color_field.module \color_field_field_validate() 1 comment

Implements hook_field_validate().

We want to verify that the color 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.field.inc, line 113

Code

function color_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
    foreach ($items as $delta => $item) {
        // Test required color fields.
        if ($instance['required'] && empty($item['rgb'])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'color_required',
                'message' => t('The color for %field is required.', array(
                    '%value' => trim($item['rgb']),
                    '%field' => $instance['label'],
                )),
                'error_element' => array(
                    'rgb' => TRUE,
                    'opacity' => FALSE,
                ),
            );
        }
        // Test required opacity fields is opacity need to be recorded.
        if ($field['settings']['opacity'] && $instance['required'] && empty($item['opacity'])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'color_required',
                'message' => t('The opacity for %field is required.', array(
                    '%value' => trim($item['rgb']),
                    '%field' => $instance['label'],
                )),
                'error_element' => array(
                    'rgb' => FALSE,
                    'opacity' => TRUE,
                ),
            );
        }
        // Test rgb field format.
        $regexp = '@^#[0-9a-fA-F]{6}$@';
        if (!empty($item['rgb']) && !preg_match($regexp, $item['rgb'])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'color_invalid',
                'message' => t('The value %value provided for %field is not a valid color.', array(
                    '%value' => trim($item['rgb']),
                    '%field' => $instance['label'],
                )),
                'error_element' => array(
                    'rgb' => TRUE,
                    'opacity' => FALSE,
                ),
            );
        }
        // Test opacity field.
        $regexp = '@^[0]?(\\.)(\\d)*$|^[01]$@';
        if (!empty($item['opacity']) && !preg_match($regexp, $item['opacity'])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'opacity_invalid',
                'message' => t('The value %value provided for %field is not a valid opacity.', array(
                    '%value' => trim($item['opacity']),
                    '%field' => $instance['label'],
                )),
                'error_element' => array(
                    'rgb' => FALSE,
                    'opacity' => TRUE,
                ),
            );
        }
    }
}