Making a Profile Field Required

In customizing your forum's registration, you may want to make certain profile fields required. In this guide, we'll make the Location field required. You can do this with any field, however. Just use the name of your field, such as "instrument," instead of "location" in the code changes below.

The are two types of required fields: fields required at registration only, and fields required in both registration and profile updates. To require a field at registration only, make this change to includes/usercp_register.php.

#
#-----[ FIND ]------------------------------------------
#
if ( empty($username) || empty($new_password) || empty($password_confirm) || empty($email) )

#
#-----[ IN-LINE FIND ]------------------------------------------
#
empty($email)

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
 
 || empty($location)

If you want to require a profile field during both registration and profile updates, make these changes to includes/usercp_register.php instead.

#
#-----[ FIND ]------------------------------------------
#
	include($phpbb_root_path . 'includes/usercp_avatar.'.$phpEx);
	$passwd_sql = '';
#
#-----[ BEFORE, ADD ]------------------------------------------
#
	if( empty($location) )
	{
		$error = TRUE;
		$error_msg .= ( ( isset($error_msg) ) ? '<br />' : '' ) . $lang['Fields_empty'];
	}

It is also a good idea to indicate that the field is now required in the registration and/or profile editing forms. You will need to edit the profile_add_body.tpl for each template installed on your forum. This tutorial will use code from the subSilver file for examples, but please be aware that the relevant code will be different on some other templates.

The first step is to find the area of the file that needs to be edited. Search for the name of the profile field you are requiring in the file. You should be able to find it in code similar to the next line, which is for the "location" field.

	  <td class="row1"><span class="gen">{L_LOCATION}:</span></td>

Now you need to find the line within this code that contains the name of the field, as displayed on the registration/profile pages. The name is usually represented by {L_SOMETHING}, like {L_LOCATION}. Look for a colon, : - this will follow the name in most cases.

If you are only requiring the field during registration, you need to add the following code after the name and colon. Please note that the first and last lines of this new code must be on lines separate from all other code in this file.

<!-- BEGIN switch_user_logged_out -->
 *
<!-- END switch_user_logged_out -->

Here is an example of the fully edited location field, when required at registration only.

	  <td class="row1"><span class="gen">{L_LOCATION}:</span></td>
<!-- BEGIN switch_user_logged_out -->
 *
<!-- END switch_user_logged_out -->

When requiring the field during both registration and profile updates, the BEGIN and END lines are not needed. You can simply add the " *" after the name and colon. Let's look at an example of this.

	  <td class="row1"><span class="gen">{L_LOCATION}: *</span></td>