From eecab1083a718ccf18e6451df6108803f4d0c1be Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Thu, 26 Mar 2009 13:31:28 +0000 Subject: #369964 by yched and bjaspan: Refactor field validation and error reporting. Field API no longer coupled to Form API. Hooray. --- modules/field/field.api.php | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'modules/field/field.api.php') diff --git a/modules/field/field.api.php b/modules/field/field.api.php index 57ec7fdd2..ef08b0b75 100644 --- a/modules/field/field.api.php +++ b/modules/field/field.api.php @@ -215,11 +215,25 @@ function hook_field_load($obj_type, $object, $field, $instance, $items) { * The instance structure for $field on $object's bundle. * @param $items * $object->{$field['field_name']}, or an empty array if unset. - * @param $form - * The form structure being validated. NOTE: This parameter will - * become obsolete (see field_attach_validate()). - */ -function hook_field_validate($obj_type, $object, $field, $instance, $items, $form) { + * @param $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. + */ +function hook_field_validate($obj_type, $object, $field, $instance, $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'])), + ); + } + } + } } /** @@ -388,6 +402,24 @@ function hook_field_widget(&$form, &$form_state, $field, $instance, $items, $del return $element; } +/** + * Flag a field-level validation error. + * + * @param $element + * An array containing the form element for the widget. The error needs to be + * flagged on the right sub-element, according to the widget's internal + * structure. + * @param $error + * An associative array with the following key-value pairs, as returned by + * hook_field_validate(): + * - 'error': the error code. Complex widgets might need to report different + * errors to different form elements inside the widget. + * - 'message': the human readable message to be displayed. + */ +function hook_field_widget_error($element, $error) { + form_error($element['value'], $error['message']); +} + /** * @} End of "ingroup field_type" */ @@ -425,7 +457,7 @@ function hook_field_attach_load($obj_type, $object) { * * See field_attach_validate() for details and arguments. */ -function hook_field_attach_validate($obj_type, $object, &$form) { +function hook_field_attach_validate($obj_type, $object, &$errors) { } /** -- cgit v1.2.3