Tuesday, January 7, 2014

Liferay Form Validator

Objective:

Perform form validation in liferay development using liferay from validator.

Liferay Form Validator:

This is simple AUI java script validator which is written by liferay people to do form validations. This script inherit the base properties from pure AUI form Validator

This java script implemented in liferay-form.js file in Liferay

Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1

Note:

The code will work for portal 6.2 version you can try for 6.1 too.

Download Liferay Form Validator portlet from following location

You can find source and war file


Portlet Screen:



Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as AUI Form Validator.

The following are the steps to use Liferay form validator in development
  1. Include AUI tag in JSP page
  2. Load liferay-form java script using AUI use method
  3. Define Liferay.Form.register instance to passing required properties


Include AUI tag in JSP page

We already have AUI jsp tag we need to include this then we can get AUI tags support to JSP page

The following is include AUI tag in JSP

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

Load liferay-form java script using AUI use method

Liferay have implemented form validation java script in liferay-form.js  file so we need to load this java script using AUI use method as follows


<aui:script>
AUI().use('aui-base','liferay-form',function(A){

//write code here

});
</aui:script>


Define Liferay.Form.register instance to passing required properties

Now we need register form using Liferay.Form.register method and we need specify the form id and required validation rules as follows

The following is simple code

<aui:input name="userName" value='' label="User Name"></aui:input>
<aui:script>
AUI().use('aui-base','liferay-form',function(A){
            Liferay.Form.register(
                        {
                                    id: '<portlet:namespace/>fm2',
                                    fieldRules: [
                                    {
                                    body: '',
                                    custom: false,
                                    errorMessage: '',
                                    fieldName: '<portlet:namespace/>userName',
                                    validatorName:'alphanum'
                                    },
                    {
                                    body: '',
                                    custom: false,
                                    errorMessage: '',
                                    fieldName: '<portlet:namespace/>userName',
                                    validatorName:'required'
                                    },

                      ]     
                                     
                        });
            });
</aui:script>

id : form id
fieldRules: is array of rules and each rule have some information about rule

Field Rule Anatomy

Example for field rule as follows

{
body: ' ',
custom: false,
errorMessage: '',
fieldName: '<portlet:namespace/>userName',
validatorName:'alphanum'
}

body: in this we pass some  custom data .
custom: it specify whether it is default rule or custom rule.
errorMessage: to specify error message if we not mention for default rule then it will take default error message.
validatorName: this name validator


The following are default Validators provided by liferay


alpha, required, equalTo, min, max, number, maxLength, minLength, url, email, alphanum, date


Writing custom Validator

Some time we need write our own validation rules for the form then we have to do like as follows.
When we want use custom validation then we have to specify custom property value as true then validator consider that is custom validation..

The following is simple example assume user age must more than 18 years then we have to 
write as follows and we need to specify custom property value as true.

<aui:input name="age" value='' label="Age"></aui:input>

The following is sample custome rule for age more than 18 years

<aui:script>
AUI().use('aui-base','liferay-form',function(A){
            Liferay.Form.register(
                        {
                                    id: '<portlet:namespace/>fm2',
                                    fieldRules: [
                                    {
                                    body:function (val, fieldNode, ruleValue) {
                                                          var result = false;
                                                               if (val >=18) {
                                                               result = true;
                                                                        }
                                                               return result;
                                                               },
                                    custom:true,
                                    errorMessage:'age must more than 18 years',
                                    fieldName: '<portlet:namespace/>age',
                                    validatorName:'customerRuleForAge'
                                    },
                      ]     
                                     
                        });
            });
</aui:script>



The following is complete JSP code

<%@ include file="init.jsp"%>
<h1>Liferay Form Validation using Liferay Validator</h1>
<aui:form name="fm2" id="fm2" action="" method="post">
            <aui:input name="userName" value='' label="User Name"></aui:input>
            <aui:input name="firstName" value='' label="First Name"></aui:input>
            <aui:input name="lastName" value='' label="Last Name"></aui:input>
            <aui:input name="email" value='' label="Email"></aui:input>
            <aui:input name="date" value='' label="Date of Birth"></aui:input>
            <aui:input name="phoneNumber" value='' label="Phone Number"></aui:input>
            <aui:input name="price" value='' label="Price Range"></aui:input>
            <aui:input name="password1" id="password1" value='' label="Password" type="password"></aui:input>
            <aui:input name="password2" value='' label="Re Type Password" type="password"></aui:input>
            <aui:input name="webSite" value='' label="Website Address"></aui:input>
            <aui:input name="age" value='' label="Age"></aui:input>
            <aui:button type="submit" name="numberSearch" value="Save" />
</aui:form>
<aui:script>
AUI().use('aui-base','liferay-form',function(A){
            Liferay.Form.register(
                        {
                                    id: '<portlet:namespace/>fm2',
                                    fieldRules: [
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                          fieldName: '<portlet:namespace/>lastName',
                                                validatorName: 'alpha'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>password1',
                                                validatorName: 'required'
                                                },
                                                {
                                                body: '#<portlet:namespace/>password1',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>password2',
                                                validatorName: 'equalTo'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>password2',
                                                validatorName: 'required'
                                                },
                                                {
                                                body: 200,
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>price',
                                                validatorName: 'min'
                                                },
                                                {
                                                body: 1000,
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>price',
                                                validatorName: 'max'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>price',
                                                validatorName: 'number'
                                                },
                                                {
                                                body: 10,
                                                custom: false,
                                                errorMessage: '',
                                          fieldName: '<portlet:namespace/>phoneNumber',
                                                validatorName: 'maxLength'
                                                },
                                                {
                                                body: 8,
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>phoneNumber',
                                                validatorName: 'minLength'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>phoneNumber',
                                                validatorName: 'digits'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>webSite',
                                                validatorName: 'url'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>email',
                                                validatorName: 'email'
                                                },
                                                {
                                                body: function (val, fieldNode, ruleValue) {
                                                                        var result = false;
                                                                        if (val >=18) {
                                                                        result = true;
                                                                        }
                                                return result;
                          },
                                                custom: true,
                                           errorMessage: 'Age must More than 18 years',
                                                fieldName: '<portlet:namespace/>age',
                                                validatorName: 'custom_rule_age'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>userName',
                                                validatorName: 'alphanum'
                                                                                    },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>userName',
                                                validatorName: 'required'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>date',
                                                validatorName: 'date'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>firstName',
                                                validatorName: 'alpha'
                                                },
                                                {
                                                body: '',
                                                custom: false,
                                                errorMessage: '',
                                                fieldName: '<portlet:namespace/>firstName',
                                                validatorName: 'required'
                                                }
                                                ]
                                   
                        });
            });
</aui:script>

Related Articles


Author

Meera Prince

Monday, January 6, 2014

Liferay Android SDK for Mobile Application Development

Introduction

Liferay is very robust web portal in the market which follows JSR 168 and JSR 286 standards.

Liferay has introduced Liferay Android SDK to develop applications using Liferay portal web service.

Liferay is supporting JSON web service from which we can develop mobile applications.
Liferay have service builder tool for creating web services for liferay applications. We simple call this web service so that we can get dynamic data that we can display in Mobiles Applications. This is very useful when we develop dynamic mobile applications.

Liferay have SOAP and REST supported web services.

If we want used HTML5 Mobile apps for Cross platform then we can Use PhoneGap

PhoneGap is frame work from which we can design mobile apps using HTML5 and Java Script. These applications can run in any mobile platforms.

This phone gap provide flexibility to create Mobile Application for any mobile platform like Android, iOS, Black Berry and Firefox Mobile OS

We can use Ajax in HTML5 to consume Restful web services.

We already have very rich java script libraries for Ajax like Jquery, DOJO, YUI and AUI.

In any application only one thing is consuming web services.

The following is location where we can get more information about PhoneGap


The following is location you can get Liferay Android SDK and Sample Application



The following is Liferay Android Tutorial


The following is create web services and using liferay web services in other platforms







Author
Meera Prince

Friday, January 3, 2014

Simple Way to Use Forms in Liferay Web Content

Objective:

Use forms in liferay web content.

When we need any form submission we need to create form in portlet and later we need maintain this data in one place but this can be done by developer but administrator can’t this.

So for this reason we need to embed some forms in web content of liferay. Create forms and manage forms one place and using form in any web applications.

HubSpot is one application where we can create forms and manage forms. Once we create form we can embed this forms in anywhere in the application.

Steps to implement
  1. Register with HubSpot and get 30 days trail account
  2. Create form in HubSpot
  3. Get Form Embedded Java Script from HubSpot
  4. Use embed Java Script in Liferay Web Content
  5. Manage From Submission Data in HubSpot
Register with HubSpot and get 30 days trail account

HubSpot one of the web applications so that we can create forms and manages forms. Internally it used Orbeon Form Builder Open Source for create forms and manage forms.

Go to following web site and click on Free Trail Account


The following screen is Home page of HubSpot



Once you click on registration screen then you will get screen will ask information as follows



Once you successfully created account you will get 30days free trail so that you can explore more here.

Here we will consider only one part that is Form Creation, Manage Forms and Using Form from HubSpot.

Create form in HubSpot

Once you get account we need to login to website as soon as we login we will navigate to our account home page and we have many menu option in navigation. We have dashboard there we can see all things at one place

The following is Dashboard Screen



Now go to Right side Vertical menu and click on Forms Option as follows




Once we click on form then we can navigate form build screen where we can create the forms.

The following is form builder screen


Now create form according to our requirement. We need to Click Create New Form button.
Once we click then we can see form builder so that it having many option to create forms. It also provides some default templates to ready use and we can customize.

Assume we will create one Contact from

Once we will click on create new form it will ask the form name as follows


Once we enter form name and we can select either form build from scratch or create from existed templates. Choice is up you.

 Assume we will select the option start from Templates.

The following screen is creating form using templates and we have two templates from which we have selected basic contact from. Once select then click on Create from

The following is screen show from creation from template



Once we selected template and click on create from then we can see form builder tool which have basic contact from template. Now we can add some new fields then we can save the form and we can publish the form.

We have different buttons Save, Preview and Actions



Once we design is completed then we need to save the form and before we can see the preview also.

Get Form Embedded Java Script from HubSpot

As of now we have designed the contact from now we need to get embedded java script code so that we can use this code in any application or any html page.

For those wee need click on Action button and we need select embed option from Actions

The following screen shows Action button and embed option



Once we click on embed option we will get Pop Up there you can see the Java Script Code. This code can be used in Html page or application.

The following screen shows form embed Java Script


Note:

We can give redirect URL so that after form submission it will navigate to specified web URL.

Use embed Java Script in Liferay Web Content

Now we already create the form in HubSpot and we also get the from embed Java Script then we will use this embed Java Script in liferay web content.

Now create web content in liferay using Web Content Display portlet

Login as Admin in liferay portal and go to add in the dock bar and add Web Content Display portlet to page.

The following screen shows after admin login into liferay portal.


The following screen is after add Web Content Display portlet to page



Now click on Add web content icon now we will get the CK Editor to design Content

The following is CK Editor to add web content in liferay


Now we will design the web content and we need to add Form Embed Java Script to this web content.

For this we need to choose CK Editor Source option so that we can see web content as HTML.
Once we choose Ck Editor Source now paste the Form Embed Java Script code in the HTML

The following is Add From Embed Java Script to Liferay Web Content.


Once we done click on save then form will be available in Web Content Display portlet then we can submit form.

The following screen is From in Liferay web content display portlet



Manage From Submission Data in HubSpot

Well we already done all the steps now where we can see the form data?

Now login into HubSpot in the top navigation select Contacts and then choose Lists

The following screen shows you List option in HubSpot.



Once we click on List we can see all froms that we designed. If we click on any for respetcive submissions you can see.

The following screen shows you list of forms



When we click on form then we can see all submissions as follows


When we click on individual item we can see the data of individual submission as follows



Note:

This is start point I just showed how use forms in liferay web content we can explore more and all the best.

We can use this Form Embed Java Script in any HTML page. This form helps us more in Static websites.

Important Points

HubSpot is application there we can create forms, manage form submission data and use forms in other applications.

We can use Form Embed Java Script in any HTML page so that we can submit the data. The submission data will be manages in HubSpot.

HubSpot uses the Orbeon from Builder tool for create and manage forms and it complete open source java based technology.

We can also integrate Orbeon from in Liferay for more details go to Orbeon website


Interesting one liferay also used HubSpot from in their website.
The following is link to see HubSpot forms in liferay.



Author
Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts