Saturday, June 13, 2015

Validation in Liferay

Validation in liferay is very easy, with Validator Class. Validator class provide us a lot of methods that we can use on any our custom fields, where we can add some of our logic to use that.
So, here is an example for 'Validator' Class.

--Create a Liferay Project in Eclipse and paste this code(please modify the package and java file name according to your project).

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");
}
 }
}
- See more at: http://liferayiseasy.blogspot.in/2014/10/validation-in-liferay.html#sthash.oqlmA0Iv.dpuf

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");
 }
 }
}
Explaination:
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");
}
 }
}
- See more at: http://liferayiseasy.blogspot.in/2014/10/validation-in-liferay.html#sthash.oqlmA0Iv.dpuf

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

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");
}
 }
}
- See more at: http://liferayiseasy.blogspot.in/2014/10/validation-in-liferay.html#sthash.oqlmA0Iv.dpuf
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");
}
 }
}
- See more at: http://liferayiseasy.blogspot.in/2014/10/validation-in-liferay.html#sthash.oqlmA0Iv.dpuf
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");
}
 }
}
- See more at: http://liferayiseasy.blogspot.in/2014/10/validation-in-liferay.html#sthash.oqlmA0Iv.dpuf