Community Documentation

v3 Knowledgebase

Adding required fields to registration form

It is possible to add an required field into the registration process without altering the core code. For this example, I chose the zip/postal code as the new registration item. You could use other fields with changes to the code below.

It would require three plugins.

First you must make the input field appear on your registration form. For this you can use the hook at:


Place this code into the plugin:
<div class="table">
    <div class="table_left">
        <label for="postal_code">*ZIP/Postal Code:</label>
    <div class="table_right">
        <input id="postal_code" type="text" size="10" value="" name="val[postal_code]">
    <div class="clear"></div>

Note that I used an asterisk before the label to show that it is required instead of the {required} label. If you have defined some other character as the required character then it can be changed to what you have defined.

Now if you look at the registration form you will see the zip field, but if you enter information into the form, it will not process. We have add some code to the function that processes the data.

To do this we must add another plugin to the hook:


This hook is located in the add user function after the user_field table has been created. (Postal_code is stored in the user_field table.)

Within this plugin add this code:

'postal_code' => $aVals['postal_code']), 'user_id = ' $iIdfalse); 

Now, if a user registers with the zip code information entered, it will be saved and will show up in their profile settings.

Everything is good so far, but it still doesn't require him to enter the zip code. For this we need to tweak the validation object a little.

To do this, create the third plugin tied to the hook:


Place this code within the hook:

$aValidation Phpfox::getService('user.register')->getValidation();
$aValidation['postal_code'] = 'Postal Code is Required';

$oValid Phpfox::getLib('validator')->set(array(
'sFormName' => 'js_form',    
'aParams' => $aValidation

The first line gets the normal validation fields for a user registration. The second line adds our new zip code field to the array of elements to be validated. It then sets these parameters into the validation object.

Now, when a user registers, he cannot successfully do so without entering something into the zip code field.

This was developed on Phpfox v3.7.