Liferay Create Account Customization by hook

Customization

Aim of this article:

Customize Liferay Create account functionality using Liferay hooks.

Sign In Portlet
Sign In Portlet
Create Account
Create Account

 Problem:

Look the above screen. The above screen is the create account User Interface. Sometimes the fields available in the create account user interface is not enough. Lets consider we need one extra field called Social Security Number in the above interface. That means at the time of creating new account the user will be able to see an extra field called Social Security Number. The user will enter the new field value and it will be persisted in the database.  We need also the validation for the new field.

Solution of the Problem:

To solve this problem below are the concepts implemented.

JSP Hook:

A jsp hook is created to customize the user interface.

Custom Field:

Only showing the extra field in the user interface is not enough. We need to persist the data. We have used custom field for persisting the data. From the control panel we have created the custom field.

Use of liferay-ui:custom-attribute tag:

In the JSP hook this tag is used to capture the user input for Social Security Number.

struts-action hook:

This hook is used to override the struts action while submitting create account form. This hook is required for server side validation of newly added field called Social Security Number.

Language Hook:

This is optional. We have created this hook for providing proper label of custom field created in control panel.

We will explain all the details step by step

Step 1: Creation of custom Field

In our case we are dealing with user registration. Therefore we are going to create a new custom field for the Entity User. Create the custom field from control panel as below. Our key is ssn.

Add Custom Field for User
Add Custom Field for User

Custom Field Permission:

The custom field is added. But one thing to remember we have to set appropriate permission for the custom field. Because the guest user i.e., at the time of creating new account this field will be viewed and updated.Set the permission as shown in the screen shot

Custom Field Permission
Custom Field Permission                 

 


 

Note

 Note: Once we add custom field for an entity Liferay will automatically assign label for it. This label can be customized by using language property hook. In the language hook put ssn as key and give meaningful label. For example

ssn= Social Security Number

Once you deploy the hook the label will be changed.


Step 2: Create language hook. The key should be ssn in the language file

ssn label before hook
ssn label before hook

After deploying the language hook

ssn label after the hook
ssn label after the hook

Step 3: Create JSP hook

create_account.jsp is the JSP file where we are going to add new field called Social Security Number. Below is the code added in the JSP.

<div class="exp-ctrl-holder">
    <liferay-ui:custom-attribute
        className="<%= User.class.getName() %>"
        classPK="<%= 0 %>"
        editable="<%= true %>"
        label="<%= true %>"
        name="ssn"
    />
</div>

The above code will render one input box with label Social Security Number. Just remember  the name attribute in the above code we have given the same key ssn as we entered in the control panel in the Step 1. The benefit of this tag is that we don’t need to do any other thing to persist the data. Once the form is submitted the data entered in the expando field will be also saved in the expando column in the database. After deploying the hook the create account form looks like below

New field added
New field added

 

Step 4: Validation of the newly added field

We have added the field and now we are able to persist the data also. But we need to do some server side validation also. Say SSN should be always numeric. To achieve this we can override struts action by hook. Below is the custom action class 

package com.proliferay.demo;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;

/**
 *
 * @author Hamidul Islam
 *
 * Discover more in
 * www.proliferay.com
 *
 */

public class CustomCreateAccountAction extends BaseStrutsPortletAction{

    /**
     * This is the custom process action
     *
     * In this process action we are reading custom field which is entered from create account form
     *
     * After reading the custom field we are calling the original process action
     */
    public void processAction(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse)
        throws Exception {
        
        
        String ssnValue = (String)PortalUtil.getExpandoValue(actionRequest, "ExpandoAttribute--" + "ssn" + "--", ExpandoColumnConstants.STRING, ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_TEXT_BOX);
        System.out.println("##################ssnValue###########################################"+ssnValue);
        if(!Validator.isNumber(ssnValue)){
            SessionErrors.add(actionRequest,"wrong-ssn");
            return;
        }
        
        
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, actionRequest,
            actionResponse);
    }
    
    
    /**
     * After process action this method is invoked
     *
     * From inside of this method we are again calling the original render method
     */
    public String render(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws Exception {

        return originalStrutsPortletAction.render(
                originalStrutsPortletAction, portletConfig, renderRequest, renderResponse);

    }

}


NoteNote 1: We overridden  struts /login/create_account

Note 2: To read custom attribute value we have used  PortalUtil.getExpandoValue

Note 3: Our custom field key is ssn. When its rendered by liferay-ui:custom-attribute the name of the field would be like this

ExpandoAttribute–ssn–

Note 4: We have added the below code in create_account.jsp for displaying error message

<liferay-ui:error key=”wrong-ssn” message=”The SSN number is not correct”/>


The final liferay-hook.xml looks like

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">

<hook>
    <language-properties>
        content/language.properties
    </language-properties>
    <custom-jsp-dir>/custom_jsps</custom-jsp-dir>
    <struts-action>
        <struts-action-path>/login/create_account</struts-action-path>
        <struts-action-impl>com.proliferay.demo.CustomCreateAccountAction</struts-action-impl>
    </struts-action>
</hook>

Download Source Code

About The Author

13 thoughts on “Liferay Create Account Customization by hook”

  1. Nice descripiton for adding custome fields.
    Can we add “Address” section of My Account portlet to Create Account Page?

  2. Why we need to override render method in CustomCreateAccountAction class ?

    I tried Without Overriding render method and i deployed it when i click on create account it was not forwarding ………….

    when i overriden the render method it was forwarding…..

  3. Hi, I am using a proccess as this one in my portal, but I need save some extra information in my data base once a user has been register sucessfully, but I do not know how can I know in the proccessAction if the user was successfully created. Some one know how can I do that ? Thanks.

  4. is there a way ,where i can see the validation besides the ssn text box ?. validation like “first name” or “email address”.

    1. Did you manage to do it?
      I am trying to find a similar solution to this.

      Problem with current validation is that it is displayed at the top of the page.

  5. Hi…..
    I am KiranPillai here,I am Web developer,Working with HTML,CSS, Javascript,JQuery and its related plugins.From Past one year i am working with liferay as well,So i am working on Liferay Theme, Hooks, Layouts,Portlets(JSP).
    I don’t know java. Now i would like to implement one scenario in liferay.

    Here It is…..

    I want to add 15 new fields to my user account creation page in liferay?How it is possible other than custom fields,
    expando tables?
    Because i know how to use custom fields.I have more fields so it will be difficult to manage.
    Through hook i want to add more fields to account creation page.

    Fields are related to user

    create one selectbox that will have list of organizations available in my application.

    create one selectbox that will have list of roles available in my application.

    onclicking of save button that should redirect to 3rd party payment gateway….. once paid amount it will create
    account for that user.
    after paid amount all the above fields need to add under extra table with related columns and maps to the user_ table with userId as a primary key.
    assign that organization to the user which is selected above and similary for role also.

    I am using liferay 6.2.

    Please Let me know if any one have any ideas………..

  6. Anyone know how do i change the ssn to Social Security Number. The tutorial is not here. I am not sure how to do that.

  7. Hi Hamidul,
    I am Rashmi here. working on Liferay. Iam facing an issue .
    I logged in as admin and I have created new site site under organization org1, assigned the users user1 to it. Is there a way to restrict the same site should not be available to admin of other organization org2 ?
    I tried to create private/restricted site under org1 but its still appearing for the org2. Is there a way to restrict the site1 should not be available to Org2.
    Second issue, is there a way to restrict the users existing under org1 should not be available to admin of org2.

  8. How can I run downloaded code. It is not being compiled. It may need some jar.

    Could you please describe the procedure of run.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d