function field_default_validate in Drupal 7
Generic field validation handler.
Possible error codes:
- 'field_cardinality': The number of values exceeds the field cardinality.
Parameters
$entity_type: The type of $entity.
$entity: The entity for the operation.
$field: The field structure for the operation.
$instance: The instance structure for $field on $entity's bundle.
$langcode: The language associated to $items.
$items: $entity->{$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 entity. 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.
See also
_hook_field_validate()
1 invocation of field_default_validate()
- field_attach_validate in modules/
field/ field.attach.inc - Perform field validation against the field data in an entity.
File
- modules/
field/ field.default.inc, line 74 - Default 'implementations' of hook_field_*(): common field housekeeping.
Code
function field_default_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// Filter out empty values.
$items = _field_filter_items($field, $items);
// Check that the number of values doesn't exceed the field cardinality.
// For form submitted values, this can only happen with 'multiple value'
// widgets.
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($items) > $field['cardinality']) {
$errors[$field['field_name']][$langcode][0][] = array(
'error' => 'field_cardinality',
'message' => t('%name: this field cannot hold more than @count values.', array(
'%name' => $instance['label'],
'@count' => $field['cardinality'],
)),
);
}
}