Saturday, August 20, 2016

Customize Validation message in Liferay

In the previous tutorial we provide server side validation in liferay.
Today we will customize the error message that is generated by liferay ui tags.So basically we provide our custom css to the error messages. We convert this:-


to this 







So lets start this Step by Step:-

Step 1:-Change the Controller
You can do all steps as in my previous blog Validaion in Liferay.
and just change the Controller as:-

Registration.java
package com.registration;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
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.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);
List<String>errors = validate(user, request);
if(errors.size() == 0){
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);
request.setAttribute("errors", errors);
}
}
public List<String> validate(RegistrationBean user,ActionRequest request){
List<String>errors = new ArrayList<String>();
ResourceBundle bundle= getPortletConfig().getResourceBundle(request.getLocale());
if(!Validator.isName(user.getFirstName())){
errors.add(bundle.getString("firstName.errorMsg.missing"));
}
if(!Validator.isName(user.getLastName())){
errors.add(bundle.getString("lastName.errorMsg.missing"));
}
if(!Validator.isEmailAddress(user.getEmail())){
errors.add(bundle.getString("email.errorMsg.missing"));
}
if(!Validator.isPhoneNumber(String.valueOf(user.getPhone()))){
errors.add(bundle.getString("phone.errorMsg.missing"));
}
return errors;
}
}


Explanation:-
1)Previously our validate method return boolean now validate method return a list which contain the error messages. Here we fetch the values from Language.properties  file by using ResourceBundle object. 

2)Register method just check the size of Error list. If there is no error than success message is print otherwise set the list in request object and send to jsp.


Step 2:-Change the view
In the view.jsp we simply iterate the list send by controller.

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>
<div id="error">
<c:if test="${!empty errors}">
<ul>
<c:forEach items="${errors}" var="error" >
<li style="color: red"><c:out value="${error}"></c:out></li>
</c:forEach>
</ul>
</c:if>
</div>
<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:-
From line 10 to 16 we just Check that if list is not empty than we can iterate the list and provide the red color to error messages.


Step 3:-Check the output

Deploy the portlet and check the output.




No comments:

Post a Comment

Total Pageviews

1039421

Number Of Unique Visitor

Free counters!