Store
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:

user.template_default_block_register_step2_7

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

user.service_process_add_feed

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:

PHP:
$this->database()->update(Phpfox::getT('user_field'), 
array(
'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:

user.component_controller_register_2

Place this code within the hook:

PHP:
$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.