diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-03-26 13:31:28 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-03-26 13:31:28 +0000 |
commit | eecab1083a718ccf18e6451df6108803f4d0c1be (patch) | |
tree | c97f1d2d434aa21fe84c9b202d264d0d7574860c /modules/field/field.api.php | |
parent | 87f82a61271b6f22eb0de8476e1b32dcbb4755f1 (diff) | |
download | brdo-eecab1083a718ccf18e6451df6108803f4d0c1be.tar.gz brdo-eecab1083a718ccf18e6451df6108803f4d0c1be.tar.bz2 |
#369964 by yched and bjaspan: Refactor field validation and error reporting. Field API no longer coupled to Form API. Hooray.
Diffstat (limited to 'modules/field/field.api.php')
-rw-r--r-- | modules/field/field.api.php | 44 |
1 files changed, 38 insertions, 6 deletions
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'])), + ); + } + } + } } /** @@ -389,6 +403,24 @@ function hook_field_widget(&$form, &$form_state, $field, $instance, $items, $del } /** + * 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) { } /** |