diff options
Diffstat (limited to 'includes/common.inc')
-rw-r--r-- | includes/common.inc | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/includes/common.inc b/includes/common.inc index 423486e94..5dddd790c 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -57,6 +57,7 @@ function drupal_get_messages() { return $messages; } + /* @} */ /** @@ -1012,8 +1013,34 @@ function form($form, $method = "post", $action = NULL, $attributes = NULL) { return "<form action=\"$action\" method=\"$method\"". drupal_attributes($attributes) .">\n$form\n</form>\n"; } -function form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE) { - return theme("form_element", $title, $value, $description, $id, $required); +/** + * File an error against the form with the specified name. + */ +function form_set_error($name, $message) { + $GLOBALS['form'][$name] = $message; + drupal_set_message($message, 'error'); +} + +/** + * Return true when errors have been set. + */ +function form_has_errors() { + return isset($GLOBALS['form']); +} + +/** + * Return the error message filed against the form with the specified name. + */ +function _form_get_error($name) { + return $GLOBALS['form'][$name]; +} + +function _form_get_class($name, $required, $error) { + return $name. ($required ? ' required' : '') . ($error ? ' error' : ''); +} + +function form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) { + return theme("form_element", $title, $value, $description, $id, $required, $error); } function form_group($legend, $group, $description = NULL) { @@ -1021,11 +1048,11 @@ function form_group($legend, $group, $description = NULL) { } function form_radio($title, $name, $value = 1, $checked = 0, $description = NULL, $attributes = NULL, $required = FALSE) { - $element = "<input type=\"radio\" class=\"form-radio\" name=\"edit[$name]\" value=\"$value\"". ($checked ? " checked=\"checked\"" : "") . drupal_attributes($attributes) .' />'; + $element = "<input type=\"radio\" class=\"". _form_get_class('form-radio', $required, _form_get_error($name)) ."\" name=\"edit[$name]\" value=\"$value\"". ($checked ? " checked=\"checked\"" : "") . drupal_attributes($attributes) .' />'; if (!is_null($title)) { $element = "<label class=\"option\">$element $title</label>"; } - return theme('form_element', NULL, $element, $description, $required); + return theme('form_element', NULL, $element, $description, $required, _form_get_error($name)); } function form_radios($title, $name, $value, $options, $description = NULL, $required = FALSE) { @@ -1034,16 +1061,16 @@ function form_radios($title, $name, $value, $options, $description = NULL, $requ foreach ($options as $key => $choice) { $choices .= "<label class=\"option\"><input type=\"radio\" class=\"form-radio\" name=\"edit[$name]\" value=\"$key\"". ($key == $value ? " checked=\"checked\"" : "") ." /> $choice</label><br />"; } - return theme('form_element', $title, $choices, $description, $required); + return theme('form_element', $title, $choices, $description, $required, _form_get_error($name)); } } function form_checkbox($title, $name, $value = 1, $checked = 0, $description = NULL, $attributes = NULL, $required = FALSE) { - $element = "<input type=\"checkbox\" class=\"form-checkbox\" name=\"edit[$name]\" id=\"edit-$name\" value=\"". $value ."\"". ($checked ? " checked=\"checked\"" : "") . drupal_attributes($attributes) .' />'; + $element = "<input type=\"checkbox\" class=\"". _form_get_class('form-checkbox', $required, _form_get_error($name)) ."\" name=\"edit[$name]\" id=\"edit-$name\" value=\"". $value ."\"". ($checked ? " checked=\"checked\"" : "") . drupal_attributes($attributes) .' />'; if (!is_null($title)) { $element = "<label class=\"option\">$element $title</label>"; } - return form_hidden($name, 0) . theme('form_element', NULL, $element, $description, $required); + return form_hidden($name, 0) . theme('form_element', NULL, $element, $description, $required, _form_get_error($name)); } function form_checkboxes($title, $name, $values, $options, $description = NULL, $required = FALSE) { @@ -1055,24 +1082,24 @@ function form_checkboxes($title, $name, $values, $options, $description = NULL, foreach ($options as $key => $choice) { $choices .= "<label class=\"option\"><input type=\"checkbox\" class=\"form-checkbox\" name=\"edit[$name][]\" value=\"$key\"". (in_array($key, $values) ? " checked=\"checked\"" : "") ." /> $choice</label><br />"; } - return theme('form_element', $title, $choices, $description, $required); + return theme('form_element', $title, $choices, $description, $required, _form_get_error($name)); } } function form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) { $size = $size ? " size=\"$size\"" : ""; - return theme("form_element", $title, "<input type=\"text\" maxlength=\"$maxlength\" class=\"form-text\" name=\"edit[$name]\" id=\"$name\"$size value=\"". check_form($value) ."\"". drupal_attributes($attributes) ." />", $description, $name, $required); + return theme("form_element", $title, "<input type=\"text\" maxlength=\"$maxlength\" class=\"". _form_get_class('form-text', $required, _form_get_error($name)) ."\" name=\"edit[$name]\" id=\"$name\"$size value=\"". check_form($value) ."\"". drupal_attributes($attributes) ." />", $description, $name, $required, _form_get_error($name)); } function form_password($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) { $size = $size ? " size=\"$size\"" : ""; - return theme("form_element", $title, "<input type=\"password\" class=\"form-password\" maxlength=\"$maxlength\" name=\"edit[$name]\" id=\"$name\"$size value=\"". check_form($value) ."\"". drupal_attributes($attributes) ." />", $description, $name, $required); + return theme("form_element", $title, "<input type=\"password\" class=\"". _form_get_class('form-password', $required, _form_get_error($name)) ."\" maxlength=\"$maxlength\" name=\"edit[$name]\" id=\"$name\"$size value=\"". check_form($value) ."\"". drupal_attributes($attributes) ." />", $description, $name, $required, _form_get_error($name)); } function form_textarea($title, $name, $value, $cols, $rows, $description = NULL, $attributes = NULL, $required = FALSE) { $cols = $cols ? " cols=\"$cols\"" : ""; module_invoke_all("textarea", $name); // eg. optionally plug in a WYSIWYG editor - return theme("form_element", $title, "<textarea wrap=\"virtual\"$cols rows=\"$rows\" name=\"edit[$name]\" id=\"$name\"". drupal_attributes($attributes) .">". check_form($value) ."</textarea>", $description, $name, $required); + return theme("form_element", $title, "<textarea wrap=\"virtual\"$cols rows=\"$rows\" name=\"edit[$name]\" id=\"$name\"". drupal_attributes($attributes) .">". check_form($value) ."</textarea>", $description, $name, $required, _form_get_error($name)); } /** @@ -1102,11 +1129,11 @@ function form_select($title, $name, $value, $options, $description = NULL, $extr $select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($value == $key ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</option>"; } } - return theme("form_element", $title, "<select name=\"edit[$name]". ($multiple ? "[]" : "") ."\"". ($multiple ? " multiple=\"multiple\" " : "") . ($extra ? " $extra" : "") ." id=\"$name\">$select</select>", $description, $name, $required); + return theme("form_element", $title, "<select name=\"edit[$name]". ($multiple ? "[]" : "") ."\"". ($multiple ? " multiple=\"multiple\" " : "") . ($extra ? " $extra" : "") ." id=\"$name\">$select</select>", $description, $name, $required, _form_get_error($name)); } function form_file($title, $name, $size, $description = NULL, $required = FALSE) { - return theme("form_element", $title, "<input type=\"file\" class=\"form-file\" name=\"edit[$name]\" id=\"$name\" size=\"$size\" />\n", $description, $name, $required); + return theme("form_element", $title, "<input type=\"file\" class=\"". _form_get_class('form-file', $required, _form_get_error($name)) ."\" name=\"edit[$name]\" id=\"$name\" size=\"$size\" />\n", $description, $name, $required, _form_get_error($error)); } function form_hidden($name, $value) { |