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






Liferay Message Customization

Today we Customize liferay Error and Success Messages . Here If user provide hello as name and hello as password success message is shown otherwise error message is shown


Step 1:-Create Liferay Project 

In eclipse click  File-->New Liferay Plugin Project-->Give Project Name --> Finish

Step 2:-Create Liferay Portlet in the project 

Then Right Click on Project-->New  Liferay Portlet-->Give the portlet Class-->And select Generic Portlet-->next







Then check create resource bundle file check box


then give the Display Category and finish




Step 3:- Add the following code in 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 var="actionUrl" ></portlet:actionURL>

<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="error"/>

<form action="<%=actionUrl %>" method="post">
Name:<input type="text" name="name" ><br>
Password:<input type="password" name="pass"><br>
<input type="submit" value="Submit">
</form>


Explanation of view.jsp

Here we use
<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="Error"/>

the success-key and error-key both are set in Java Class 
the message Successfully is shown if success-key is found(in green color) and
the message Error is shown if error-key is found(in red color)

Step 4:- Add the following code in your Class

 @Override
 public void processAction(ActionRequest request, ActionResponse response)                                throws   PortletException,  IOException {
String name = ParamUtil.getString(request, "name");
String password = ParamUtil.getString(request, "pass");
if(name.equalsIgnoreCase("hello")&& password.equalsIgnoreCase("hello"))
{
SessionMessages.add(request,"success-key");
}
else{
SessionErrors.add(request, "error-key");
}
}

Explanation of Class

1) SessionMessages.add(request,"success-key");

This "success-key " is added if user enter hello as name and hello as Password and this must match in view.jsp 

<liferay-ui:success key="success-key" message="Successfully" />

2) SessionErrors.add(request, "error-key");

This "error-key" is added if user not added hello as name and hello as password this key must match in view.jsp as

<liferay-ui:error key="error-key" message="Error"/>


Step 5:- Deploy Your Portlet on server

Right click on build.xml --> Run on Ant build

Step 6:- Open Browser and hit http://localhost:8080/ 

Add your portlet on page and put hello hello



If provide any other value



Step 6:- Disable Liferay Default error message(Your Request Failed to complete)

 Add these two lines in your Class

PortletConfig portletConfig =         (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

SessionMessages.add(request, ((LiferayPortletConfig)portletConfig).getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

and again deploy



As you see only our message is shown 

 If you want that your Message is not hard Coded and come from properties file



Step:-7 Provide entries of Language.properties in portlet.xml

Their is a Language.properties file is created in content folder (Due to Step 2) provide the entry of this in portlet.xml(actually this is already done by eclipse)

<resource-bundle>content.Language</resource-bundle>

Step:-8 Provide entries in Language.properties 

Put entries in Language.properties file

Successfully=This Success Message come from Language.properties file
Error=This Error Message come from Language.properties file

These "Succeessfully" and "Error" must match with message of view.jsp as

<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="Error"/>

Step:-9 Deploy Your Portlet







So first Liferay try to find the message in Language.properties if following key is not found then show the normal message described in view.jsp ie if you delete

Successfully=This Success Message come from Language.properties file

from properties file then error message come from Language.properties file but success message is from jsp file

Success message from jsp


Error message from Language.properties file                                                                                           








HOPE THIS WILL HELP