diff options
author | Dries Buytaert <dries@buytaert.net> | 2004-05-31 09:40:56 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2004-05-31 09:40:56 +0000 |
commit | 7f08110a5e29c29812a01b9c148e52b34f3eccdf (patch) | |
tree | 3b8f757c5102baa89195a40fc9b1af0e6cef3d47 /modules | |
parent | 2954836fbacfb1da67da83756f7505f61b75ba73 (diff) | |
download | brdo-7f08110a5e29c29812a01b9c148e52b34f3eccdf.tar.gz brdo-7f08110a5e29c29812a01b9c148e52b34f3eccdf.tar.bz2 |
- Improved form handling.
+ Introduced two new functions:
1. form_set_error($name, $message): files an error against the form
element with the specified $name.
2. form_has_errors(): returns true if errors has been filed against
form elements.
+ Updated the form handling:
1. The form_ functions will add 'class="error"' when a form field
has been found to be erroneous.
2. The error message is passed to theme_form_element() when the
particular form field has been found to be erroneous.
+ I updated the user and profile module to take advantage of these new
functions.
+ IMPORTANT: the _user() hook changed. The 'validate' case should no
longer retun an error message when something goes wrong but should
set it with form_set_error().
Diffstat (limited to 'modules')
-rw-r--r-- | modules/comment.module | 2 | ||||
-rw-r--r-- | modules/comment/comment.module | 2 | ||||
-rw-r--r-- | modules/profile.module | 37 | ||||
-rw-r--r-- | modules/profile/profile.module | 37 | ||||
-rw-r--r-- | modules/user.module | 114 | ||||
-rw-r--r-- | modules/user/user.module | 114 |
6 files changed, 108 insertions, 198 deletions
diff --git a/modules/comment.module b/modules/comment.module index f5da756cf..832c5e979 100644 --- a/modules/comment.module +++ b/modules/comment.module @@ -281,7 +281,7 @@ function comment_post($edit) { ** Validate the comment's body. */ - if ($edit["comment"] == "") { + if ($edit['comment'] == '') { return array(t("Empty comment"), t("The comment you submitted is empty.")); } diff --git a/modules/comment/comment.module b/modules/comment/comment.module index f5da756cf..832c5e979 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -281,7 +281,7 @@ function comment_post($edit) { ** Validate the comment's body. */ - if ($edit["comment"] == "") { + if ($edit['comment'] == '') { return array(t("Empty comment"), t("The comment you submitted is empty.")); } diff --git a/modules/profile.module b/modules/profile.module index 7f5873d42..eef0f93a2 100644 --- a/modules/profile.module +++ b/modules/profile.module @@ -183,11 +183,11 @@ function profile_validate_profile($edit) { while ($field = db_fetch_object($result)) { if ($edit[$field->name]) { if ($field->type == 'url' && !valid_url($edit[$field->name], true)) { - return t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title)); + form_set_error($field->name, t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title))); } } else if ($field->required) { - return t("The field '%field' is required.", array('%field' => $field->title)); + form_set_error($field->name, t("The field '%field' is required.", array('%field' => $field->title))); } } @@ -215,22 +215,22 @@ function profile_validate_form($edit) { // Validate the title: if (!$edit['title']) { - return t('You must enter a title.'); + form_set_error('title', t('You must enter a title.')); } // Validate the 'form name': if (eregi('[^a-z0-9_-]', $edit['name'])) { - return t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.'); + form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.')); } if (in_array($edit['name'], user_fields())) { - return t('The specified form name is reserved for use by Drupal.'); + form_set_error('name', t('The specified form name is reserved for use by Drupal.')); } // Validate the category: if (!$edit['category']) { - return t('You must enter a category.'); + form_set_error('category', t('You must enter a category.')); } } @@ -241,16 +241,18 @@ function profile_admin_add($type) { if ($_POST['op']) { $data = $_POST['edit']; - if ($error = profile_validate_form($data)) { - drupal_set_message($error, 'error'); - } - else if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) { - drupal_set_message(t('the specified title is already in use'), 'error'); + // Validate the form: + profile_validate_form($data); + + if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) { + form_set_error('title', t('the specified title is already in use.')); } - else if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) { - drupal_set_message(t('the specified name is already in use'), 'error'); + + if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) { + form_set_error('name', t('the specified name is already in use.')); } - else { + + if (!form_has_errors()) { db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page']); drupal_set_message(t('the field has been created.')); @@ -268,11 +270,10 @@ function profile_admin_edit($fid) { if ($_POST['op']) { $data = $_POST['edit']; - if ($error = profile_validate_form($data)) { - drupal_set_message($error, 'error'); + // Validate form: + profile_validate_form($data); - } - else { + if (!form_has_errors()) { db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page'], $fid); drupal_set_message(t('the field has been updated.')); diff --git a/modules/profile/profile.module b/modules/profile/profile.module index 7f5873d42..eef0f93a2 100644 --- a/modules/profile/profile.module +++ b/modules/profile/profile.module @@ -183,11 +183,11 @@ function profile_validate_profile($edit) { while ($field = db_fetch_object($result)) { if ($edit[$field->name]) { if ($field->type == 'url' && !valid_url($edit[$field->name], true)) { - return t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title)); + form_set_error($field->name, t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title))); } } else if ($field->required) { - return t("The field '%field' is required.", array('%field' => $field->title)); + form_set_error($field->name, t("The field '%field' is required.", array('%field' => $field->title))); } } @@ -215,22 +215,22 @@ function profile_validate_form($edit) { // Validate the title: if (!$edit['title']) { - return t('You must enter a title.'); + form_set_error('title', t('You must enter a title.')); } // Validate the 'form name': if (eregi('[^a-z0-9_-]', $edit['name'])) { - return t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.'); + form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.')); } if (in_array($edit['name'], user_fields())) { - return t('The specified form name is reserved for use by Drupal.'); + form_set_error('name', t('The specified form name is reserved for use by Drupal.')); } // Validate the category: if (!$edit['category']) { - return t('You must enter a category.'); + form_set_error('category', t('You must enter a category.')); } } @@ -241,16 +241,18 @@ function profile_admin_add($type) { if ($_POST['op']) { $data = $_POST['edit']; - if ($error = profile_validate_form($data)) { - drupal_set_message($error, 'error'); - } - else if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) { - drupal_set_message(t('the specified title is already in use'), 'error'); + // Validate the form: + profile_validate_form($data); + + if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) { + form_set_error('title', t('the specified title is already in use.')); } - else if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) { - drupal_set_message(t('the specified name is already in use'), 'error'); + + if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) { + form_set_error('name', t('the specified name is already in use.')); } - else { + + if (!form_has_errors()) { db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page']); drupal_set_message(t('the field has been created.')); @@ -268,11 +270,10 @@ function profile_admin_edit($fid) { if ($_POST['op']) { $data = $_POST['edit']; - if ($error = profile_validate_form($data)) { - drupal_set_message($error, 'error'); + // Validate form: + profile_validate_form($data); - } - else { + if (!form_has_errors()) { db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page'], $fid); drupal_set_message(t('the field has been updated.')); diff --git a/modules/user.module b/modules/user.module index 498f568f0..6bf12233c 100644 --- a/modules/user.module +++ b/modules/user.module @@ -187,22 +187,20 @@ function user_validate_picture($file, &$edit, $user) { list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85')); if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) { - $error = t('The uploaded file was not an image.'); + form_set_error('picture', t('The uploaded file was not an image.')); } else if ($file->size > (variable_get('user_picture_file_size', '30') * 1000)) { - $error = t('The uploaded image is too large; the maximum file size is %a kB.', array('%a' => variable_get('user_picture_file_size', '30'))); + form_set_error('picture', t('The uploaded image is too large; the maximum file size is %a kB.', array('%a' => variable_get('user_picture_file_size', '30')))); } else if ($size[0] > $maxwidth || $size[1] > $maxheight) { - $error = t('The uploaded image is too large; the maximum dimensions are %a pixels.', array('%a' => variable_get('user_picture_dimensions', '85x85'))); + form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %a pixels.', array('%a' => variable_get('user_picture_dimensions', '85x85')))); } else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR .'picture-'. $user->uid . $extension, 1)) { $edit['picture'] = $file->path; } else { - $error = t('Failed to upload the picture image; the "%directory" directory doesn\'t exist.', array('%directory' => variable_get('user_picture_path', 'pictures'))); + form_set_error('picture', t('Failed to upload the picture image; the "%directory" directory doesn\'t exist.', array('%directory' => variable_get('user_picture_path', 'pictures')))); } - - return $error; } function user_validate_authmap($account, $authname, $module) { @@ -821,11 +819,11 @@ function user_pass($edit = array()) { if ($edit['name']) { $account = db_fetch_object(db_query("SELECT uid, name, mail FROM {users} WHERE status = 1 AND LOWER(name) = '%s'", strtolower($edit['name']))); - if (!$account) $error = t('Sorry. The username "<i>%s</i>" is not recognized.', array('%s' => $edit['name'])); + if (!$account) form_set_error('name', t('Sorry. The username "<i>%s</i>" is not recognized.', array('%s' => $edit['name']))); } else if ($edit['mail']) { $account = db_fetch_object(db_query("SELECT uid, name, mail FROM {users} WHERE status = 1 AND LOWER(mail) = '%s'", strtolower($edit['mail']))); - if (!$account) $error = t('Sorry. The e-mail address "<i>%s</i>" is not recognized.', array('%s' => $edit['mail'])); + if (!$account) form_set_error('name', t('Sorry. The e-mail address "<i>%s</i>" is not recognized.', array('%s' => $edit['mail']))); } if ($account) { @@ -853,11 +851,6 @@ function user_pass($edit = array()) { } else { - // Display error message if necessary. - if ($error) { - drupal_set_message($error, 'error'); - } - // Display form: $output .= '<p>'. t('Enter your username <strong><em>or</em></strong> your e-mail address.') .'</p>'; $output .= form_textfield(t('Username'), 'name', $edit['name'], 30, 64); @@ -883,25 +876,22 @@ function user_register($edit = array()) { if (!(is_null($edit['name']) && is_null($edit['mail']))) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (user_deny('user', $edit['name'])) { - $error = t('The name "%s" has been denied access.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" has been denied access.', array('%s' => $edit['name']))); } else if (user_deny('mail', $edit['mail'])) { - $error = t('The e-mail address "%s" has been denied access.', array('%s' => $edit['mail'])); + form_set_error('mail', t('The e-mail address "%s" has been denied access.', array('%s' => $edit['mail']))); } else if (db_num_rows(db_query("SELECT name FROM {users} WHERE LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if (db_num_rows(db_query("SELECT mail FROM {users} WHERE LOWER(mail) = LOWER('%s') OR LOWER(init) = LOWER('%s')", $edit['mail'], $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); - } - else if (variable_get('user_register', 1) == 0) { - $error = t('Public registrations have been disabled by the site administrator.'); + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } else { foreach (module_list() as $module) { @@ -910,19 +900,12 @@ function user_register($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } } - if (!$error) { - $success = true; - } } } - if ($success) { + if (!form_has_errors()) { $from = variable_get('site_mail', ini_get('sendmail_from')); $pass = user_password(); @@ -963,11 +946,6 @@ function user_register($edit = array()) { } } } - else { - if ($error) { - drupal_set_message($error, 'error'); - } - } // Display the registration form. $output .= variable_get('user_registration_help', ''); @@ -994,21 +972,21 @@ function user_edit($edit = array()) { if ($user->uid) { if (!(is_null($edit['name']) && is_null($edit['mail']))) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != $user->uid AND LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if ($edit['mail'] && db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != $user->uid AND LOWER(mail) = LOWER('%s')", $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); + form_set_name('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } else { // If required, validate the picture. if ($file = file_check_upload('picture')) { - $error = user_validate_picture($file, $edit, $user); + user_validate_picture($file, $edit, $user); } // If required, check that proposed passwords match. If so, @@ -1018,7 +996,7 @@ function user_edit($edit = array()) { $edit['pass'] = $edit['pass1']; } else { - $error = t('The specified passwords do not match.'); + form_set_error('pass2', t('The specified passwords do not match.')); } } unset($edit['pass1'], $edit['pass2']); @@ -1044,13 +1022,9 @@ function user_edit($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } - if (!$error) { + if (!form_has_errors()) { // Save user information. $user = user_save($user, array_merge($edit, $data)); @@ -1059,10 +1033,6 @@ function user_edit($edit = array()) { } } - if ($error) { - drupal_set_message($error, 'error'); - } - if (!$edit) { $edit = object2array($user); } @@ -1276,23 +1246,20 @@ function user_settings() { function user_admin_create($edit = array()) { if ($edit['name'] || $edit['mail']) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (db_num_rows(db_query("SELECT name FROM {users} WHERE LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if (db_num_rows(db_query("SELECT mail FROM {users} WHERE LOWER(mail) = LOWER('%s')", $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); - } - else { - $success = 1; + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } } - if ($success) { + if (!form_has_errors()) { watchdog('user', 'new user: "'. $edit['name'] .'" <'. $edit['mail'] .'>'); user_save('', array('name' => $edit['name'], 'pass' => $edit['pass'], 'init' => $edit['mail'], 'mail' => $edit['mail'], 'rid' => array(_user_authenticated_id()), 'status' => 1)); @@ -1300,11 +1267,6 @@ function user_admin_create($edit = array()) { drupal_set_message(t('Created a new user account. No e-mail has been sent.')); } else { - - if ($error) { - drupal_set_message($error, 'error'); - } - $output = form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Provide the username of the new account.')); $output .= form_textfield(t('E-mail address'), 'mail', $edit['mail'], 30, 55, t('Provide the e-mail address associated with the new account.')); $output .= _user_profile($edit, $edit, 'form'); @@ -1506,19 +1468,19 @@ function user_admin_edit($edit = array()) { // TODO: This display/edit/validate should be moved to a new profile // module implementing hook_user(). if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (count($edit['rid']) < 1) { - $error = t('The user must have at least one role.'); + form_set_error('rid', t('The user must have at least one role.')); } else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if ($edit['mail'] && db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(mail) = LOWER('%s')", $account->uid, $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } // Validate fields added by other modules. @@ -1529,15 +1491,11 @@ function user_admin_edit($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } // If required, validate the picture. if ($file = file_check_upload('picture')) { - $error = user_validate_picture($file, $edit, $account); + user_validate_picture($file, $edit, $account); } // If required, check that proposed passwords match. If so, @@ -1547,18 +1505,15 @@ function user_admin_edit($edit = array()) { $edit['pass'] = $edit['pass1']; } else { - $error = t('The specified passwords do not match.'); + form_set_error('pass2', t('The specified passwords do not match.')); } } unset($edit['pass1'], $edit['pass2']); - if (!$error) { + if (!form_has_errors()) { $account = user_save($account, array_merge($edit, $data)); drupal_set_message(t('user information changes have been saved.')); } - else { - drupal_set_message($error, 'error'); - } } else if ($op == t('Delete account')) { if ($edit['status'] == 0) { @@ -1570,8 +1525,7 @@ function user_admin_edit($edit = array()) { return user_admin_account(); } else { - $error = t('Failed to delete account: the account has to be blocked first.'); - drupal_set_message($error, 'error'); + drupal_set_message(t('Failed to delete account: the account has to be blocked first.'), 'error'); } } diff --git a/modules/user/user.module b/modules/user/user.module index 498f568f0..6bf12233c 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -187,22 +187,20 @@ function user_validate_picture($file, &$edit, $user) { list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85')); if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) { - $error = t('The uploaded file was not an image.'); + form_set_error('picture', t('The uploaded file was not an image.')); } else if ($file->size > (variable_get('user_picture_file_size', '30') * 1000)) { - $error = t('The uploaded image is too large; the maximum file size is %a kB.', array('%a' => variable_get('user_picture_file_size', '30'))); + form_set_error('picture', t('The uploaded image is too large; the maximum file size is %a kB.', array('%a' => variable_get('user_picture_file_size', '30')))); } else if ($size[0] > $maxwidth || $size[1] > $maxheight) { - $error = t('The uploaded image is too large; the maximum dimensions are %a pixels.', array('%a' => variable_get('user_picture_dimensions', '85x85'))); + form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %a pixels.', array('%a' => variable_get('user_picture_dimensions', '85x85')))); } else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR .'picture-'. $user->uid . $extension, 1)) { $edit['picture'] = $file->path; } else { - $error = t('Failed to upload the picture image; the "%directory" directory doesn\'t exist.', array('%directory' => variable_get('user_picture_path', 'pictures'))); + form_set_error('picture', t('Failed to upload the picture image; the "%directory" directory doesn\'t exist.', array('%directory' => variable_get('user_picture_path', 'pictures')))); } - - return $error; } function user_validate_authmap($account, $authname, $module) { @@ -821,11 +819,11 @@ function user_pass($edit = array()) { if ($edit['name']) { $account = db_fetch_object(db_query("SELECT uid, name, mail FROM {users} WHERE status = 1 AND LOWER(name) = '%s'", strtolower($edit['name']))); - if (!$account) $error = t('Sorry. The username "<i>%s</i>" is not recognized.', array('%s' => $edit['name'])); + if (!$account) form_set_error('name', t('Sorry. The username "<i>%s</i>" is not recognized.', array('%s' => $edit['name']))); } else if ($edit['mail']) { $account = db_fetch_object(db_query("SELECT uid, name, mail FROM {users} WHERE status = 1 AND LOWER(mail) = '%s'", strtolower($edit['mail']))); - if (!$account) $error = t('Sorry. The e-mail address "<i>%s</i>" is not recognized.', array('%s' => $edit['mail'])); + if (!$account) form_set_error('name', t('Sorry. The e-mail address "<i>%s</i>" is not recognized.', array('%s' => $edit['mail']))); } if ($account) { @@ -853,11 +851,6 @@ function user_pass($edit = array()) { } else { - // Display error message if necessary. - if ($error) { - drupal_set_message($error, 'error'); - } - // Display form: $output .= '<p>'. t('Enter your username <strong><em>or</em></strong> your e-mail address.') .'</p>'; $output .= form_textfield(t('Username'), 'name', $edit['name'], 30, 64); @@ -883,25 +876,22 @@ function user_register($edit = array()) { if (!(is_null($edit['name']) && is_null($edit['mail']))) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (user_deny('user', $edit['name'])) { - $error = t('The name "%s" has been denied access.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" has been denied access.', array('%s' => $edit['name']))); } else if (user_deny('mail', $edit['mail'])) { - $error = t('The e-mail address "%s" has been denied access.', array('%s' => $edit['mail'])); + form_set_error('mail', t('The e-mail address "%s" has been denied access.', array('%s' => $edit['mail']))); } else if (db_num_rows(db_query("SELECT name FROM {users} WHERE LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if (db_num_rows(db_query("SELECT mail FROM {users} WHERE LOWER(mail) = LOWER('%s') OR LOWER(init) = LOWER('%s')", $edit['mail'], $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); - } - else if (variable_get('user_register', 1) == 0) { - $error = t('Public registrations have been disabled by the site administrator.'); + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } else { foreach (module_list() as $module) { @@ -910,19 +900,12 @@ function user_register($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } } - if (!$error) { - $success = true; - } } } - if ($success) { + if (!form_has_errors()) { $from = variable_get('site_mail', ini_get('sendmail_from')); $pass = user_password(); @@ -963,11 +946,6 @@ function user_register($edit = array()) { } } } - else { - if ($error) { - drupal_set_message($error, 'error'); - } - } // Display the registration form. $output .= variable_get('user_registration_help', ''); @@ -994,21 +972,21 @@ function user_edit($edit = array()) { if ($user->uid) { if (!(is_null($edit['name']) && is_null($edit['mail']))) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != $user->uid AND LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if ($edit['mail'] && db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != $user->uid AND LOWER(mail) = LOWER('%s')", $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); + form_set_name('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } else { // If required, validate the picture. if ($file = file_check_upload('picture')) { - $error = user_validate_picture($file, $edit, $user); + user_validate_picture($file, $edit, $user); } // If required, check that proposed passwords match. If so, @@ -1018,7 +996,7 @@ function user_edit($edit = array()) { $edit['pass'] = $edit['pass1']; } else { - $error = t('The specified passwords do not match.'); + form_set_error('pass2', t('The specified passwords do not match.')); } } unset($edit['pass1'], $edit['pass2']); @@ -1044,13 +1022,9 @@ function user_edit($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } - if (!$error) { + if (!form_has_errors()) { // Save user information. $user = user_save($user, array_merge($edit, $data)); @@ -1059,10 +1033,6 @@ function user_edit($edit = array()) { } } - if ($error) { - drupal_set_message($error, 'error'); - } - if (!$edit) { $edit = object2array($user); } @@ -1276,23 +1246,20 @@ function user_settings() { function user_admin_create($edit = array()) { if ($edit['name'] || $edit['mail']) { if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (db_num_rows(db_query("SELECT name FROM {users} WHERE LOWER(name) = LOWER('%s')", $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if (db_num_rows(db_query("SELECT mail FROM {users} WHERE LOWER(mail) = LOWER('%s')", $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); - } - else { - $success = 1; + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } } - if ($success) { + if (!form_has_errors()) { watchdog('user', 'new user: "'. $edit['name'] .'" <'. $edit['mail'] .'>'); user_save('', array('name' => $edit['name'], 'pass' => $edit['pass'], 'init' => $edit['mail'], 'mail' => $edit['mail'], 'rid' => array(_user_authenticated_id()), 'status' => 1)); @@ -1300,11 +1267,6 @@ function user_admin_create($edit = array()) { drupal_set_message(t('Created a new user account. No e-mail has been sent.')); } else { - - if ($error) { - drupal_set_message($error, 'error'); - } - $output = form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Provide the username of the new account.')); $output .= form_textfield(t('E-mail address'), 'mail', $edit['mail'], 30, 55, t('Provide the e-mail address associated with the new account.')); $output .= _user_profile($edit, $edit, 'form'); @@ -1506,19 +1468,19 @@ function user_admin_edit($edit = array()) { // TODO: This display/edit/validate should be moved to a new profile // module implementing hook_user(). if ($error = user_validate_name($edit['name'])) { - // Do nothing. + form_set_error('name', $error); } else if ($error = user_validate_mail($edit['mail'])) { - // Do nothing. + form_set_error('mail', $error); } else if (count($edit['rid']) < 1) { - $error = t('The user must have at least one role.'); + form_set_error('rid', t('The user must have at least one role.')); } else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $edit['name'])) > 0) { - $error = t('The name "%s" is already taken.', array('%s' => $edit['name'])); + form_set_error('name', t('The name "%s" is already taken.', array('%s' => $edit['name']))); } else if ($edit['mail'] && db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(mail) = LOWER('%s')", $account->uid, $edit['mail'])) > 0) { - $error = t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail'])); + form_set_error('mail', t('The e-mail address "%s" is already taken.', array('%s' => $edit['mail']))); } // Validate fields added by other modules. @@ -1529,15 +1491,11 @@ function user_admin_edit($edit = array()) { if (is_array($result)) { $data = array_merge($data, $result); } - elseif (is_string($result)) { - $error = $result; - break; - } } // If required, validate the picture. if ($file = file_check_upload('picture')) { - $error = user_validate_picture($file, $edit, $account); + user_validate_picture($file, $edit, $account); } // If required, check that proposed passwords match. If so, @@ -1547,18 +1505,15 @@ function user_admin_edit($edit = array()) { $edit['pass'] = $edit['pass1']; } else { - $error = t('The specified passwords do not match.'); + form_set_error('pass2', t('The specified passwords do not match.')); } } unset($edit['pass1'], $edit['pass2']); - if (!$error) { + if (!form_has_errors()) { $account = user_save($account, array_merge($edit, $data)); drupal_set_message(t('user information changes have been saved.')); } - else { - drupal_set_message($error, 'error'); - } } else if ($op == t('Delete account')) { if ($edit['status'] == 0) { @@ -1570,8 +1525,7 @@ function user_admin_edit($edit = array()) { return user_admin_account(); } else { - $error = t('Failed to delete account: the account has to be blocked first.'); - drupal_set_message($error, 'error'); + drupal_set_message(t('Failed to delete account: the account has to be blocked first.'), 'error'); } } |