Commit 5f0e0f36 authored by Steven Wittens's avatar Steven Wittens
Browse files

- #33433: More form API fixes, and error handling improvements by Chx.

parent 09532828
......@@ -134,7 +134,7 @@ function _form_validate(&$elements) {
/* Validate the current input */
if (!$elements['#validated'] && $elements['#input']) {
if ($elements['#required'] && !$elements['#value']) {
if ($elements['#required'] && !isset($elements['#value'])) {
form_error($elements, t('%name field is required', array('%name' => $elements['#title'])));
}
if ($elements['#valid']) {
......@@ -157,16 +157,53 @@ function _form_validate(&$elements) {
}
}
/**
* File an error against a form element. If the name of the element is
* edit[foo][bar] then you may pass either foo or foo][bar as $name
* foo will set an error for all its children.
*/
function form_set_error($name = NULL, $message = NULL) {
static $form = array();
if (isset($name) && !isset($form[$name])) {
$form[$name] = $message;
drupal_set_message($message, 'error');
}
return $form;
}
/**
* Return an associative array of all errors.
*/
function form_get_errors() {
$form = form_set_error();
if (!empty($form)) {
return $form;
}
}
/**
* Return the error message filed against the form with the specified name.
*/
function form_get_error($element) {
$form = form_set_error();
$key = $element['#parents'][0];
if (isset($form[$key])) {
return $form[$key];
}
$key = implode('][', $element['#parents']);
if (isset($form[$key])) {
return $form[$key];
}
}
/**
* Flag an element as having an error.
*/
function form_error(&$element, $message) {
$element['#error'] = TRUE;
$GLOBALS['form'][$element['#name']] = $message;
drupal_set_message($message, 'error');
form_set_error(implode('][', $element['#parents']), $message);
}
/**
* Adds some required properties to each form element, which are used internally in the form api.
* This function also automatically assigns the value property from the $edit array, provided the
......@@ -348,7 +385,7 @@ function theme_select($element) {
$select .= '<option value="'. $key .'"'. (is_array($element['#value']) ? (in_array($key, $element['#value']) ? ' selected="selected"' : '') : ($element['#value'] == $key ? ' selected="selected"' : '')) .'>'. check_plain($choice) .'</option>';
}
}
return theme('form_element', $element['#title'], '<select name="'. $element['#name'] .''. ($element['#multiple'] ? '[]' : '') .'"'. ($element['#multiple'] ? ' multiple="multiple" ' : '') . ($element['#extra'] ? ' '. $element['#extra'] : '') .' id="' . $element['#id'] .'">'. $select .'</select>', $element['#description'], $element['#name'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], '<select name="'. $element['#name'] .''. ($element['#multiple'] ? '[]' : '') .'"'. ($element['#multiple'] ? ' multiple="multiple" ' : '') . ($element['#extra'] ? ' '. $element['#extra'] : '') .' id="' . $element['#id'] .'">'. $select .'</select>', $element['#description'], $element['#name'], $element['#required'], form_get_error($element));
}
/**
......@@ -386,7 +423,7 @@ function theme_fieldset($element) {
*/
function theme_radio($element) {
$output = '<input type="radio" ';
$output .= 'class="'. _form_get_class('form-radio', $element['#required'], _form_get_error($element['#name'])) .'" ';
$output .= 'class="'. _form_get_class('form-radio', $element['#required'], form_get_error($element)) .'" ';
$output .= 'name="' . $element['#name'] .'" ';
$output .= 'value="'. $element['#return_value'] .'" ';
$output .= ($element['#value'] == $element['#return_value']) ? ' checked="checked" ' : ' ';
......@@ -394,7 +431,7 @@ function theme_radio($element) {
if (!is_null($element['#title'])) {
$output = '<label class="option">'. $output .' '. $element['#title'] .'</label>';
}
return theme('form_element', NULL, $output, $element['#description'], $element['#name'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', NULL, $output, $element['#description'], $element['#name'], $element['#required'], form_get_error($element));
}
/**
......@@ -408,7 +445,7 @@ function theme_radio($element) {
*/
function theme_radios($element) {
if ($element['#title'] || $element['#description']) {
return theme('form_element', $element['#title'], $element['#children'], $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], $element['#children'], $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
}
else {
return $element['#children'];
......@@ -426,7 +463,7 @@ function theme_radios($element) {
*/
function theme_date($element) {
$output = '<div class="container-inline">' . $element['#children'] . '</div>';
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
}
/**
......@@ -512,7 +549,7 @@ function theme_item($element) {
function theme_checkbox($element) {
$checkbox = '<input ';
$checkbox .= 'type="checkbox" ';
$checkbox .= 'class="'. _form_get_class('form-checkbox', $element['#required'], _form_get_error($element['#name'])) . '" ';
$checkbox .= 'class="'. _form_get_class('form-checkbox', $element['#required'], form_get_error($element)) . '" ';
$checkbox .= 'name="'. $element['#name'] .'" ';
$checkbox .= 'id="'. $element['#id'].'" ' ;
$checkbox .= 'value="'. $element['#return_value'] .'" ';
......@@ -523,7 +560,7 @@ function theme_checkbox($element) {
$checkbox = '<label class="option">'. $checkbox .' '. $element['#title'] .'</label>';
}
return theme('form_element', NULL, $checkbox, $element['#description'], $element['#name'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', NULL, $checkbox, $element['#description'], $element['#name'], $element['#required'], form_get_error($element));
}
/**
......@@ -536,7 +573,7 @@ function theme_checkbox($element) {
*/
function theme_checkboxes($element) {
if ($element['#title'] || $element['#description']) {
return theme('form_element', $element['#title'], $element['#children'], $element['#description'], 'edit-'. $element['#name'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], $element['#children'], $element['#description'], 'edit-'. $element['#name'], $element['#required'], form_get_error($element));
}
else {
return $element['#children'];
......@@ -597,8 +634,8 @@ function theme_textfield($element) {
$extra = '<input class="autocomplete" type="hidden" id="'. $element['#id'] .'-autocomplete" value="'. check_url(url($element['#autocomplete_path'], NULL, NULL, TRUE)) .'" disabled="disabled" />';
}
$output = '<input type="text" maxlength="'. $element['#maxlength'] .'" class="'. _form_get_class("form-text$class", $element['#required'], _form_get_error($element['#name'])) .'" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' />';
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name'])). $extra;
$output = '<input type="text" maxlength="'. $element['#maxlength'] .'" class="'. _form_get_class("form-text$class", $element['#required'], form_get_error($element)) .'" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' />';
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], form_get_error($element)). $extra;
}
/**
......@@ -629,7 +666,7 @@ function theme_form($element) {
function theme_textarea($element) {
$cols = $element['#cols'] ? ' cols="'. $element['#cols'] .'"' : '';
return theme('form_element', $element['#title'], '<textarea'. $cols .' rows="'. $element['#rows'] .'" name="'. $element['#name'] .'" id="' . $element['#id'] .'" class="'. _form_get_class('textarea', $element['#required'], _form_get_error($element['#name'])) .'"'. drupal_attributes($element['#attributes']) .'>'. check_plain($element['#value']) .'</textarea>', $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], '<textarea'. $cols .' rows="'. $element['#rows'] .'" name="'. $element['#name'] .'" id="' . $element['#id'] .'" class="'. _form_get_class('textarea', $element['#required'], form_get_error($element)) .'"'. drupal_attributes($element['#attributes']) .'>'. check_plain($element['#value']) .'</textarea>', $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
}
/**
......@@ -662,9 +699,9 @@ function theme_markup($element) {
function theme_password($element) {
$size = $element['#size'] ? ' size="'. $element['#size'] .'" ' : '';
$output = '<input type="password" maxlength="'. $element['#maxlength'] .'" class="'. _form_get_class("form-text $class", $element['#required'], _form_get_error($element['#name'])) .'" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $size . drupal_attributes($element['#attributes']) .' />';
$output = '<input type="password" maxlength="'. $element['#maxlength'] .'" class="'. _form_get_class("form-text $class", $element['#required'], form_get_error($element)) .'" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $size . drupal_attributes($element['#attributes']) .' />';
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name']));
return theme('form_element', $element['#title'], $output, $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
}
/**
......@@ -686,23 +723,6 @@ function theme_weight($element) {
return form_render($element);
}
/**
* File an error against the form element with the specified name.
*/
function form_set_error($name, $message) {
$GLOBALS['form'][$name] = $message;
drupal_set_message($message, 'error');
}
/**
* Return an associative array of all errors.
*/
function form_get_errors() {
if (array_key_exists('form', $GLOBALS)) {
return $GLOBALS['form'];
}
}
/**
* Format a file upload field.
*
......@@ -723,18 +743,8 @@ function form_get_errors() {
* provided by file.inc.
*/
function theme_file($element) {
return theme('form_element', $element['#title'], '<input type="file" class="'. _form_get_class('form-file', $element['#required'], _form_get_error($element['#name'])) .'" name="'. $element['#name'] .'" id="'. form_clean_id($element['#id']) .'" size="'. $element['#size'] ."\" />\n", $element['#description'], $element['#id'], $element['#required'], _form_get_error($element['#name']));
}
/**
* Return the error message filed against the form with the specified name.
*/
function _form_get_error($name) {
if (array_key_exists('form', $GLOBALS)) {
return $GLOBALS['form'][$name];
}
return theme('form_element', $element['#title'], '<input type="file" class="'. _form_get_class('form-file', $element['#required'], form_get_error($element)) .'" name="'. $element['#name'] .'" id="'. form_clean_id($element['#id']) .'" size="'. $element['#size'] ."\" />\n", $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
}
function _form_get_class($name, $required, $error) {
return $name. ($required ? ' required' : '') . ($error ? ' error' : '');
}
......
......@@ -60,7 +60,7 @@ function _locale_admin_manage_screen() {
$ratio = ($original->strings > 0 && $translation->translation > 0) ? round(($translation->translation/$original->strings)*100., 2) : 0;
$form['name'][$key] = array('#type' => 'textfield', '#default_value' => $lang, '#size' => 15, '#maxlength' => 64);
$form['translation'][$key] = array('#type' => 'markup', '#default_value' => "$translation->translation/$original->strings ($ratio%)");
$form['translation'][$key] = array('#type' => 'markup', '#value' => "$translation->translation/$original->strings ($ratio%)");
}
}
$form['enabled'] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => $enabled, '#return_value' => 1);
......@@ -984,7 +984,7 @@ function _locale_string_edit($lid) {
: array('#type' => 'textfield', '#title' => $languages['name'][$translation->locale], '#default_value' => $translation->translation, '#size' => 60, '#maxlength' => 128);
unset($languages['name'][$translation->locale]);
}
$form = array('#type' => 'item', '#title' => t('Original text'), '#value' => wordwrap(check_plain($orig, 0)));
$form['item'] = array('#type' => 'item', '#title' => t('Original text'), '#value' => wordwrap(check_plain($orig, 0)));
foreach ($languages['name'] as $key => $lang) {
$form[$key] = (strlen($orig) > 40) ?
array('#type' => 'textarea', '#title' => $lang, '#cols' => 60, '#rows' => 15) :
......@@ -993,7 +993,7 @@ function _locale_string_edit($lid) {
$form['submit'] = array('#type' => 'submit', '#value' => t('Save translations'));
return $form;
return drupal_get_form('_locale_string_edit', $form);
}
/**
......
......@@ -108,7 +108,9 @@ function locale_user($type, $edit, &$user, $category = NULL) {
$user->language = key($languages['name']);
}
$languages['name'] = array_map('check_plain', $languages['name']);
return array(array('title' => t('Interface language settings'), 'data' => $form['locale_user'] = array('#type' => 'radios', '#title' => t('Language'), '#default_value' => $user->language, '#options' => $languages['name'], '#description' => t('Selecting a different locale will change the interface language of the site.'))));
$form['locale'] = array('#title' => t('Interface language settings'), '#type' => 'fieldset', '#weight' => 1);
$form['locale']['language'] = array('#type' => 'radios', '#title' => t('Language'), '#default_value' => $user->language, '#options' => $languages['name'], '#description' => t('Selecting a different locale will change the interface language of the site.'));
return $form;
}
}
......
......@@ -108,7 +108,9 @@ function locale_user($type, $edit, &$user, $category = NULL) {
$user->language = key($languages['name']);
}
$languages['name'] = array_map('check_plain', $languages['name']);
return array(array('title' => t('Interface language settings'), 'data' => $form['locale_user'] = array('#type' => 'radios', '#title' => t('Language'), '#default_value' => $user->language, '#options' => $languages['name'], '#description' => t('Selecting a different locale will change the interface language of the site.'))));
$form['locale'] = array('#title' => t('Interface language settings'), '#type' => 'fieldset', '#weight' => 1);
$form['locale']['language'] = array('#type' => 'radios', '#title' => t('Language'), '#default_value' => $user->language, '#options' => $languages['name'], '#description' => t('Selecting a different locale will change the interface language of the site.'));
return $form;
}
}
......
......@@ -1083,6 +1083,7 @@ function node_types_configure($type = NULL) {
$form['workflow'] = array('#type' => 'fieldset', '#title' =>t('Workflow'));
$form['workflow'] = array_merge($form['workflow'], node_invoke_nodeapi($node, 'settings'));
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
return system_settings_form($type . '_node_settings', $form);
}
else {
......
......@@ -1083,6 +1083,7 @@ function node_types_configure($type = NULL) {
$form['workflow'] = array('#type' => 'fieldset', '#title' =>t('Workflow'));
$form['workflow'] = array_merge($form['workflow'], node_invoke_nodeapi($node, 'settings'));
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
return system_settings_form($type . '_node_settings', $form);
}
else {
......
......@@ -298,7 +298,7 @@ function profile_view_profile($user) {
$description = ($field->visibility == PROFILE_PRIVATE) ? t('The content of this field is private and only visible to yourself.') : '';
$title = ($field->type != 'checkbox') ? check_plain($field->title) : '';
$form = array('#type' => 'item', '#title' => $title, '#value' => $value, '#description' => $description);
$fields[$field->category][$field->name] = form_render(_form_builder($form));
$fields[$field->category][$field->name] = drupal_get_form('profile_view_profile_' . $field->category .'_'. $field->name, $form, 'profile_view_profile');
}
}
......
......@@ -298,7 +298,7 @@ function profile_view_profile($user) {
$description = ($field->visibility == PROFILE_PRIVATE) ? t('The content of this field is private and only visible to yourself.') : '';
$title = ($field->type != 'checkbox') ? check_plain($field->title) : '';
$form = array('#type' => 'item', '#title' => $title, '#value' => $value, '#description' => $description);
$fields[$field->category][$field->name] = form_render(_form_builder($form));
$fields[$field->category][$field->name] = drupal_get_form('profile_view_profile_' . $field->category .'_'. $field->name, $form, 'profile_view_profile');
}
}
......
......@@ -188,7 +188,7 @@ function system_user($type, $edit, &$user, $category = NULL) {
$form['locale'] = array('#type'=>'item', '#title' => t('Locale settings'), '#weight' => 6);
$form['locale']['timezone'] = array(
'#type' => 'select', '#title' => t('Time zone'), '#default_value' => strlen($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0),
'#options' => $zones, descriptions => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')
'#options' => $zones, '#description' => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')
);
}
return $form;
......@@ -684,7 +684,9 @@ function system_settings_form($form_id, $form) {
/**
* Execute the system_settings_form.
*
* Due to specific handling of checkboxes, this function does not handle 'tree' based forms.
* If you want node type configure style handling of your checkboxes,
* add an array_filter value to your form.
*
*/
function system_settings_form_execute($form_id, $values) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
......@@ -694,7 +696,7 @@ function system_settings_form_execute($form_id, $values) {
variable_del($key);
}
else {
if (is_array($value)) {
if (is_array($value) && isset($values['array_filter'])) {
$value = array_keys(array_filter($value));
}
variable_set($key, $value);
......@@ -1057,11 +1059,14 @@ function system_theme_settings($key = '') {
$disabled['toggle_search'] = true;
}
$form['toggles'] = array('#type' => 'fieldset', '#title' => t('Toggle display'), '#description' => t('Enable or disable the display of certain page elements.'));
$form['theme_settings'] = array('#type' => 'fieldset', '#title' => t('Toggle display'), '#description' => t('Enable or disable the display of certain page elements.'), '#tree' => TRUE);
foreach ($toggles as $name => $title) {
if ((!$key) || in_array($name, $features)) {
// disable search box if search.module is disabled
$form['toggles'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $settings[$name], '#attributes' => isset($disabled[$name]) ? array('disabled' => 'disabled') : NULL);
$form['theme_settings'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $settings[$name]);
if (isset($disabled[$name])) {
$form['theme_settings'][$name]['#attributes'] = array('disabled' => 'disabled');
}
}
}
......
......@@ -188,7 +188,7 @@ function system_user($type, $edit, &$user, $category = NULL) {
$form['locale'] = array('#type'=>'item', '#title' => t('Locale settings'), '#weight' => 6);
$form['locale']['timezone'] = array(
'#type' => 'select', '#title' => t('Time zone'), '#default_value' => strlen($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0),
'#options' => $zones, descriptions => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')
'#options' => $zones, '#description' => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')
);
}
return $form;
......@@ -684,7 +684,9 @@ function system_settings_form($form_id, $form) {
/**
* Execute the system_settings_form.
*
* Due to specific handling of checkboxes, this function does not handle 'tree' based forms.
* If you want node type configure style handling of your checkboxes,
* add an array_filter value to your form.
*
*/
function system_settings_form_execute($form_id, $values) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
......@@ -694,7 +696,7 @@ function system_settings_form_execute($form_id, $values) {
variable_del($key);
}
else {
if (is_array($value)) {
if (is_array($value) && isset($values['array_filter'])) {
$value = array_keys(array_filter($value));
}
variable_set($key, $value);
......@@ -1057,11 +1059,14 @@ function system_theme_settings($key = '') {
$disabled['toggle_search'] = true;
}
$form['toggles'] = array('#type' => 'fieldset', '#title' => t('Toggle display'), '#description' => t('Enable or disable the display of certain page elements.'));
$form['theme_settings'] = array('#type' => 'fieldset', '#title' => t('Toggle display'), '#description' => t('Enable or disable the display of certain page elements.'), '#tree' => TRUE);
foreach ($toggles as $name => $title) {
if ((!$key) || in_array($name, $features)) {
// disable search box if search.module is disabled
$form['toggles'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $settings[$name], '#attributes' => isset($disabled[$name]) ? array('disabled' => 'disabled') : NULL);
$form['theme_settings'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $settings[$name]);
if (isset($disabled[$name])) {
$form['theme_settings'][$name]['#attributes'] = array('disabled' => 'disabled');
}
}
}
......
......@@ -476,7 +476,7 @@ function user_user($type, &$edit, &$user, $category = NULL) {
}
if ($type == 'validate' && $category == 'account') {
return user_edit_validate(arg(1), $edit);
return _user_edit_validate(arg(1), $edit);
}
if ($type == 'categories') {
......@@ -1172,7 +1172,7 @@ function user_edit_form($uid, $edit) {
return $form;
}
function user_edit_validate($uid, &$edit) {
function _user_edit_validate($uid, &$edit) {
// Validate the username:
if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error);
......
......@@ -476,7 +476,7 @@ function user_user($type, &$edit, &$user, $category = NULL) {
}
if ($type == 'validate' && $category == 'account') {
return user_edit_validate(arg(1), $edit);
return _user_edit_validate(arg(1), $edit);
}
if ($type == 'categories') {
......@@ -1172,7 +1172,7 @@ function user_edit_form($uid, $edit) {
return $form;
}
function user_edit_validate($uid, &$edit) {
function _user_edit_validate($uid, &$edit) {
// Validate the username:
if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment