- Profile module improvement:

* Made it possible to set certain fields as 'required'.

  To do:
   * Mark them as required visually.
   * Add required fields to the subscription page.
4.5.x
Dries Buytaert 2004-03-27 14:50:56 +00:00
parent 3fbfc52030
commit 5f17ad3c2d
6 changed files with 54 additions and 31 deletions

View File

@ -325,6 +325,7 @@ CREATE TABLE profile_fields (
page varchar(255) default NULL,
type varchar(128) default NULL,
weight tinyint(1) DEFAULT '0' NOT NULL,
required tinyint(1) DEFAULT '0' NOT NULL,
overview tinyint(1) DEFAULT '0' NOT NULL,
options text,
KEY category (category),

View File

@ -52,7 +52,8 @@ $sql_updates = array(
"2004-02-03" => "update_78",
"2004-02-21" => "update_79",
"2004-03-11: first update since Drupal 4.4.0 release" => "update_80",
"2004-02-20" => "update_81"
"2004-02-20" => "update_81",
"2004-02-27" => "update_82"
);
function update_32() {
@ -890,6 +891,11 @@ function update_81() {
return $ret;
}
function update_82() {
$ret[] = update_sql("ALTER TABLE {profile_fields} ADD required tinyint(1) DEFAULT '0' NOT NULL");
return $ret;
}
function update_sql($sql) {
$edit = $_POST["edit"];
$result = db_query($sql);

View File

@ -185,6 +185,9 @@ function profile_validate_profile($edit) {
return 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));
}
}
return $edit;
@ -240,7 +243,7 @@ function profile_admin_add($type) {
drupal_set_message($error, 'error');
}
else {
db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['overview'], $data['options'], $data['page']);
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.'));
}
@ -262,7 +265,7 @@ function profile_admin_edit($fid) {
}
else {
db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['overview'], $data['options'], $data['page'], $fid);
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.'));
}
@ -291,6 +294,7 @@ Unless you know what you are doing, it is highly recommended that you prefix the
$group .= form_textarea(t('Selection options'), 'options', $edit['options'], 70, 8, t("A list of all options. Put each option on a separate line. Example options are 'red', 'blue', 'green', etc."));
}
$group .= form_weight(t('Weight'), 'weight', $edit['weight'], 5, t("The weights define the order in which the form fields are shown. Lighter fields \"float up\" towards the top of the category."));
$group .= form_checkbox(t('Required field.'), 'required', 1, $edit['required']);
$output = form_group(t('Field settings'), $group);
$group = '';

View File

@ -185,6 +185,9 @@ function profile_validate_profile($edit) {
return 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));
}
}
return $edit;
@ -240,7 +243,7 @@ function profile_admin_add($type) {
drupal_set_message($error, 'error');
}
else {
db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['overview'], $data['options'], $data['page']);
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.'));
}
@ -262,7 +265,7 @@ function profile_admin_edit($fid) {
}
else {
db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['overview'], $data['options'], $data['page'], $fid);
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.'));
}
@ -291,6 +294,7 @@ Unless you know what you are doing, it is highly recommended that you prefix the
$group .= form_textarea(t('Selection options'), 'options', $edit['options'], 70, 8, t("A list of all options. Put each option on a separate line. Example options are 'red', 'blue', 'green', etc."));
}
$group .= form_weight(t('Weight'), 'weight', $edit['weight'], 5, t("The weights define the order in which the form fields are shown. Lighter fields \"float up\" towards the top of the category."));
$group .= form_checkbox(t('Required field.'), 'required', 1, $edit['required']);
$output = form_group(t('Field settings'), $group);
$group = '';

View File

@ -1512,19 +1512,6 @@ function user_admin_edit($edit = array()) {
if ($account = user_load(array('uid' => $id))) {
if ($op == t("Save account")) {
foreach (module_list() as $module) {
if (module_hook($module, 'user')) {
$result = module_invoke($module, 'user', 'validate', $edit, $account);
}
if (is_array($result)) {
$data = array_merge($data, $result);
}
elseif (is_string($result)) {
$error = $result;
break;
}
}
// TODO: this display/edit/validate should be moved to a new profile module implementing the _user hooks
if ($error = user_validate_name($edit['name'])) {
// do nothing
@ -1539,6 +1526,23 @@ function user_admin_edit($edit = array()) {
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit['mail']));
}
/*
** Validate fields added by other modules.
*/
foreach (module_list() as $module) {
if (module_hook($module, 'user')) {
$result = module_invoke($module, 'user', 'validate', $edit, $account);
}
if (is_array($result)) {
$data = array_merge($data, $result);
}
elseif (is_string($result)) {
$error = $result;
break;
}
}
/*
** If required, validate the picture.
*/

View File

@ -1512,19 +1512,6 @@ function user_admin_edit($edit = array()) {
if ($account = user_load(array('uid' => $id))) {
if ($op == t("Save account")) {
foreach (module_list() as $module) {
if (module_hook($module, 'user')) {
$result = module_invoke($module, 'user', 'validate', $edit, $account);
}
if (is_array($result)) {
$data = array_merge($data, $result);
}
elseif (is_string($result)) {
$error = $result;
break;
}
}
// TODO: this display/edit/validate should be moved to a new profile module implementing the _user hooks
if ($error = user_validate_name($edit['name'])) {
// do nothing
@ -1539,6 +1526,23 @@ function user_admin_edit($edit = array()) {
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit['mail']));
}
/*
** Validate fields added by other modules.
*/
foreach (module_list() as $module) {
if (module_hook($module, 'user')) {
$result = module_invoke($module, 'user', 'validate', $edit, $account);
}
if (is_array($result)) {
$data = array_merge($data, $result);
}
elseif (is_string($result)) {
$error = $result;
break;
}
}
/*
** If required, validate the picture.
*/