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
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
Thanks you for reading my blog. Please comment below if you like my post.
0 comments:
Post a Comment