- 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().
4.5.x
Dries Buytaert 2004-05-31 09:40:56 +00:00
parent 2954836fba
commit 7f08110a5e
9 changed files with 154 additions and 213 deletions

View File

@ -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) {

View File

@ -269,11 +269,12 @@ function theme_node($node, $main = 0, $page = 0) {
* @param $value the form element's data
* @param $description the form element's description or explanation
* @param $id the form element's ID used by the &lt;label&gt; tag
* @param $required a boolean to indicate whether this is a required field or not
* @param $error a string with an error message filed against this form element
*
* @return a string representing the form element
*/
function theme_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE) {
function theme_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {
$output = "<div class=\"form-item\">\n";

View File

@ -132,6 +132,9 @@ td.menu-disabled {
.form-item {
margin-bottom: 1em;
}
.form-item input.error {
border: 2px solid red;
}
.form-item .description {
font-size: 0.85em;
}

View File

@ -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."));
}

View File

@ -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."));
}

View File

@ -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');
// 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 title = '%s'", $data['title']))) {
drupal_set_message(t('the specified title 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 (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');
}
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.'));

View File

@ -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');
// 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 title = '%s'", $data['title']))) {
drupal_set_message(t('the specified title 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 (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');
}
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.'));

View File

@ -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'] .'" &lt;'. $edit['mail'] .'&gt;');
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');
}
}

View File

@ -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'] .'" &lt;'. $edit['mail'] .'&gt;');
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');
}
}