hook_field_validate
- Versions
- 7
hook_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors)
Define custom validate behavior for this module's field types.
Parameters
$obj_type The type of $object.
$object The object for the operation. Note that this might not be a full-fledged 'object'. When invoked through field_attach_query(), the $object will only include properties that the Field API knows about: bundle, id, revision id, and field values (no node title, user name...).
$field The field structure for the operation.
$instance The instance structure for $field on $object's bundle.
$langcode The language associated to $items.
$items $object->{$field['field_name']}[$langcode], or an empty array if unset.
$errors The array of errors, keyed by field name and by value delta, that have already been reported for the object. The function should add its errors to this array. Each error is an associative array, with the following keys and values:
- 'error': an error code (should be a string, prefixed with the module name)
- 'message': the human readable message to be displayed.
Related topics
Code
modules/field/field.api.php, line 369
<?php
function hook_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['value'])) {
if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
$errors[$field['field_name']][$delta][] = array(
'error' => 'text_max_length',
'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
);
}
}
}
}
?>Login or register to post comments 