Pages

Friday, 27 June 2014

AUI form validations


Liferay has provided default validations which you dont have to write any script in your file
These are the examples

You have to ADD this Taglib before creating the form

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

Example 1:
<aui:input name="firstName"  label="Name">
 <aui:validator name="required"/>
 <aui:validator name="alpha"/>
</aui:input>

 Explanation:
  required: Tells that user must fill this field
                                          message: This field is required
alpha :tells that user must enter alpha character i.e spaces between words are not allowed
                                           message: please enter alpha characters only.


Example 2:

<aui:input name="Name" value='' label="Name">
 <aui:validator name="alphanum"/>
</aui:input>
  Explanation: 
  alphanum :This will accept alphabets and numbers
                                             message:Please enter only alphanumeric characters.
                                         

  Example 3: 
<aui:input name="Address"  type="textarea" label="Name">
 <aui:validator name="required"/>
 <aui:validator name="maxLength">160</aui:validator>
<aui:validator name="minLength">5</aui:validator> 
</aui:input>
Explanation:   
 maxLength:tells that user has to enter less than 160 characters in textarea
                                               message:Please enter no more than 160 characters.
 minLength: tells the user to enter the minimum x characters
                                               message:Please enter at least 5 characters.                                   


  Example 4:
<aui:input name="date" label="Date of Birth">
 <aui:validator name="date"></aui:validator>
</aui:input>
Explanation:   
 date: tells to the user to enter the valid date
                                                  message:Please enter a valid date                    

  Example 5:
<aui:input name="age"  label="age">
<aui:validator name="number"></aui:validator>
<aui:validator name="max">35</aui:validator>
<aui:validator name="min">18</aui:validator>
 <aui:validator name="digits"></aui:validator>
</aui:input>
Explanation:   
number:Tells to the user to enter only numbers
                                                   message:Please enter a valid number
 max:Tells the user to enter the number not more than 35
                                                   message:Please enter a value less than or equal to35
 min:Tells the user to enter the number not less than 18
                                                  message:Please enter a value greater than or equal to18
 digits:Tells to user to enter only digits
                                                   message:Please enter only digits

 Example 6: EMAIL


<aui:input name="email" value='' label="Email">
 <aui:validator name="email"/>
</aui:input>  
Explanation:  
email:Tells to the user entered email is in valid format or not.
                                                message:Please enter a valid email address.

  Example 6: Password Checker
<aui:input name="password1" id="password1" label="Password">
 <aui:validator name="required"></aui:validator>
</aui:input>
<aui:input name="password2" label="Re Type Password">
 <aui:validator name="equalTo">'#<portlet:namespace />password1'</aui:validator>
</aui:input> 
  Explanation:
equalTo:Check for the password same or not
                                               message:Please enter the same value again.

Example :7
 Validator for uploading the files
   <aui:validator name="acceptFiles">'jpg,png,tif,gif'</aui:validator>
Tells that user have has to upload the files that are specified in validator
                                                 message: Please enter a value with a valid extension

Custom message:

 If you are not satisfied with the messages give by liferay fallow the below process for all the above examples

<aui:input name="name"  label="Age">
 <aui:validator  name="required"  errorMessage="You must fill this field" >
 <aui:validator name="maxLengtherrorMessage="you have entered more than 160 characters it should be less than 160" >160</aui:validator>
</aui:validator>
</aui:input>

 If you want to write your own conditions to your text field fallow the below process


<aui:input name="marks"  label="marks">
 <aui:validator  name="custom"  errorMessage="the student is failed" >
function (val, fieldNode, ruleValue) {
var result = false;
if (val >35) {
result = true;
}
return result;
}
</aui:validator>
</aui:input> 

Explanation: 
For custom validation you have to write your script function inside the validator tag as mentioned  above