Monday, August 15, 2016

Validation in Liferay using liferay-ui tags

Today we will discuss server side validation in Liferay. Here we create a simple form with fields like First name, Last name, email and phone.If there is some error then we show error messages otherwise print Success message on console.For validation we use Validator class and <liferay-ui> tags. So lets start this Step by Step:-

Step 1:-Create Liferay Project and Portlet
Just create a Liferay project and then create a MVC portlet in it. While creating portlet check the create resource bundle file checkbox so that it will create Language.properties file and provide entry of this file in portlet.xml.




Step 2:-Change view.jsp
Open view.jsp and paste this :-

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<portlet:defineObjects />
<portlet:actionURL var="register" name="register">
</portlet:actionURL>
<liferay-ui:error key="firstName.errorMsg.missing" message="firstName.errorMsg.missing" />
<liferay-ui:error key="lastName.errorMsg.missing" message="lastName.errorMsg.missing"/>
<liferay-ui:error key="phone.errorMsg.missing" message="phone.errorMsg.missing"/>
<liferay-ui:error key="email.errorMsg.missing" message="email.errorMsg.missing"/>
<form action="${register}" method="post" >
<div>
<label>First Name <sup>*</sup> : </label><input type="text" value='<c:out value="${user.firstName}"></c:out>' name="firstName">
</div>
<div>
<label>Last Name <sup>*</sup> : </label><input type="text" value='<c:out value="${user.lastName}"></c:out>' name="lastName">
</div>
<div>
<label>Phone <sup>*</sup> : </label><input type="text" value='<c:out value="${user.phone}"></c:out>' name="phone">
</div>
<div>
<label>E-mail ID <sup>*</sup> : </label><input type="text" value='<c:out value="${user.email}"></c:out>' name="email">
</div>
<div>
<label>&nbsp;</label><input type="submit" value="Submit">
</div>
</form>
view raw view.jsp hosted with ❤ by GitHub

Explanation:-
1)Here we create a simple form that contain 4 fields firstName, lastName,phone and email.
2)We also use liferay-ui tags and jstl tags so import both the taglibs.
3)Here we are using 
          <liferay-ui:error key="" message="" />

key:- Key part comes from Controller
message :- Message part comes Language.properties

So provide entries in Language.properties

firstName.errorMsg.missing=Please enter First Name.
lastName.errorMsg.missing=Please enter Last Name.
email.errorMsg.missing=Please enter Email.
phone.errorMsg.missing=Please enter Phone Number.

4)As we are using jstl tags so we need add the jars in our liferay-plugin-package.properties file:-








Step 3:-Create Bean Class
We have 4 fields in our jsp so create a bean ie RegistrationBean and create getter and setters in it:-

RegistrationBean.java
package com.registration;
public class RegistrationBean {
String firstName,lastName,phone,email;
public RegistrationBean(String firstName, String lastName, String phone,String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}


Step 4:-Change required name space parameter
As we are not using AUI tage so make required name space parameter to false so just provide the entry in liferay-portlet.xml :-

<requires-namespaced-parameters>
                         false
</requires-namespaced-parameters>


Step 5:-Change the Controller
Open your java class and paste this:-

Registration.java
package com.registration;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
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.JavaConstants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Registration extends MVCPortlet {
public void register(ActionRequest request, ActionResponse response) {
String firstName = ParamUtil.getString(request, "firstName");
String lastName = ParamUtil.getString(request,"lastName");
String phone = ParamUtil.getString(request,"phone");
String email = ParamUtil.getString(request,"email");
RegistrationBean user = new RegistrationBean(firstName, lastName, phone, email);
if(validate(user, request)){
System.out.println("Successfully Created");
}else{
PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, ((LiferayPortletConfig)portletConfig).getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
request.setAttribute("user", user);
}
}
public boolean validate(RegistrationBean user,ActionRequest request){
boolean flag = true;
if(!Validator.isName(user.getFirstName())){
SessionErrors.add(request, "firstName.errorMsg.missing");
flag =false;
}
if(!Validator.isName(user.getLastName())){
SessionErrors.add(request,"lastName.errorMsg.missing");
flag =false;
}
if(!Validator.isEmailAddress(user.getEmail())){
SessionErrors.add(request,"email.errorMsg.missing");
flag =false;
}
if(!Validator.isPhoneNumber(String.valueOf(user.getPhone()))){
SessionErrors.add(request,"phone.errorMsg.missing");
flag =false;
}
return flag;
}
}

Explanation:-
1)In the validate method we use Validator Class methods like  isEmailAddress() and then add this in SessionErrors add method like :-
SessionErrors.add(request, "firstName.errorMsg.missing");

The add method of SessionErrors take 2 arguments first is ActionRequest object and Second is Key. This key must match in jsp <liferay-ui:error key="firstName.errorMsg.missing" />

2)In register method we fetch the values and create a bean object and then pass to validate method .If there is no error just print a message on console otherwise disable the default error message (line 26 to line 27) and add the user object in request so that we can autofill the correct fields in jsp using jstl tags.








Step 6:-Check the output
Deploy the portlet and check the output as:-





In the next blog we Customize this Customize the validation message


Project Structure:-







Hope this will Help....

You can Download Source code from  Validation in Liferay using liferay-ui tags.


Related Post:-




No comments:

Post a Comment

Total Pageviews

1039514

Number Of Unique Visitor

Free counters!