Sunday, October 26, 2014

Validation In Liferay

Validation in Liferay by using Validator Class Provided by Liferay


Validation in Liferay is very simple by using Validator Class. Validator Class give all the usefull method that can be used for validation. You can also use your logic for validation. In this tutorial i use both approach.

Here we create a simple form that contain a text field for phone number we validate that phone number must contain Numbers and not less than 10 digits if error occur again this form is shown with already filled previous value otherwise go to success page.



Step 1 :- Create liferay project in java file paste the content 

package com.test;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class ValidationDemo extends MVCPortlet {
 public void inClass(ActionRequest request,ActionResponse  response) throws PortletException, IOException
 {
String phoneNumber = ParamUtil.getString(request, "phone");
SessionMessages.add(request, ((LiferayPortletConfig)getPortletConfig()).getPortletId() + SessionMessages. KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
 
if(!Validator.isDigit(phoneNumber))
{
SessionErrors.add(request, "notNumber");
}
if(phoneNumber.length()!=10)
{
SessionErrors.add(request, "lessLength");
}
request.setAttribute("phoneNumber", phoneNumber);
 
if(SessionErrors.isEmpty(request))
{
response.setRenderParameter("jspPage","/html/validationdemo/success.jsp");
}
 }
}


Explanation:-

1)Using Validator Class:-we use Validator.isDigit(String) method to check that Phone number must be between 0-9 and not contain a-z orA-Z.

2)Using Custom Logic:-In (phoneNumber.length()!=10) we check that Phone number must contain 10 digits.

In both we add error key in SessionsError

3)If error occur we send back the Phone number to view.jsp so that it fill in the text box again 
          request.setAttribute("phoneNumber", phoneNumber);

4)If no error occur we send response to success.jsp





Step 2:- Paste Content to view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<portlet:defineObjects />
<portlet:actionURL name="inClass" var="injsp"></portlet:actionURL>

<liferay-ui:error key="notNumber" message="Phone Number not Contain Alphabets"/>
<liferay-ui:error key="lessLength" message="Phone Number Must be of 10 Digit"/>

<form action="${injsp}" method="post" >
Phone Number:<input type="text" name="phone" value="${phoneNumber}"><br>
<input type="submit" value="Submit">
</form>

Explanation:-

1) The Error keys map with Corresponding messages

<liferay-ui:error key="notNumber" message="Phone Number not Contain Alphabets"/>
<liferay-ui:error key="lessLength" message="Phone Number Must be of 10 Digit"/>

2)In input box we set the value of phone number send by request

<input type="text" name="phone" value="${phoneNumber}">

Step 3:- Paste Content to success.jsp

<h1>Success Fully Submitted</h1>

Step 4:-Deploy your Project and see output














Hope This Help


Related Post:-






No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!