In post this we will customize Liferay Create account functionality using Liferay hooks.
In this page we have to add a field. We will add a field named as "Social Security Number"
Step 1: Creation of custom Field
We are customizing 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.
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
Step 2: Create language hook in our example-hook project. The key should be ssn in the language file .
Put ssn=Social Security Number in language_en.properties file
Before deploy language hook
After deploy language hook
Step 3: Create JSP hook
Open create_account.jsp and add this code
The above code will render one input box with label Social Security Number.
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.
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.
Creating Struts action
Open CustomCreateAccountAction.java file and put this code
Note 1: We overridden struts /login/create_account
Note 2: To read custom attribute value we have used PortalUtil.getExpandoValue
The final liferay-hook.xml
In this page we have to add a field. We will add a field named as "Social Security Number"
Step 1: Creation of custom Field
We are customizing 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.
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
Step 2: Create language hook in our example-hook project. The key should be ssn in the language file .
Put ssn=Social Security Number in language_en.properties file
Before deploy language hook
After deploy language hook
Step 3: Create JSP hook
Open create_account.jsp and add this code
<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.
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.
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.
Creating Struts action
Open CustomCreateAccountAction.java file and put this code
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);
}
}
Note 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
<?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>
No comments:
Post a Comment