function field_example_field_validate in Examples for Developers 7
Implements hook_field_validate().
This hook gives us a chance to validate content that's in our field. We're really only interested in the $items parameter, since it holds arrays representing content in the field we've defined. 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
field_example_field_widget_error()
Related topics
File
- field_example/
field_example.module, line 79 - An example field using the Field Types API.
Code
function field_example_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['rgb'])) {
if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'field_example_invalid',
'message' => t('Color must be in the HTML format #abcdef.'),
);
}
}
}
}