Thursday, October 14, 2010

Custom Validation Components

Custom Validation Components :


If you want validated  email ,zip code and other  type of validation which are dependent on the application then you should need to create the custom validation

Following step are depict the way of implementing  validation

1-First you create a class which should be implement the Validation interface
Description Of the Validator Interface
  Validator interface have only one method
  Name : public void validate(FacesContext arg0, UIComponent arg1, .Object arg2) throws javax.faces.validator.ValidatorException;
}
And this method have three argument which are following type
  1-FacesContext
  2-UIcomponent
  3-Object
public class ValidationExample implements Validator {

}
Step 2: Implement the validate method
the interface have one method so we should need to override this method
Pattern partten=Pattern.compile(number);
Matcher mat=partten.matcher(number);
if(mat.matches()){
}
else{
FacesMessage message=new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Value Should be number and also lenght should be 10");
message.setDetail("Value Should be number and also lenght should be 10");
throw new ValidatorException(message);
}
}

Step 3: Register your custom validator with the FacesContext
The code to register the custom validator with the FacesContext should look familiar to you by now.



<validator>
<validator-id>ValidationExample</validator-id>
<validator-class>ValidationExample</validator-class>
</validator>


Step 4: Use the tag in your JSPs

Custom Converters

Custom Converters :
This type of converter is base on the requirment of the application .if you want to convert any object in the basic of the application object then you should need to implement this way .you should follow these following step

1-fist create class which should be implement the Converter interface .
   Interface name:Converter
   package name :javax.faces.converter
   method description :
         this class have two method
                1-getAsObject()
                2-getAsString()

2-after first step implenment getAsobject .this method is responsible to convert the string(user in put value ) into any type of object(Application specific) .


3-implement the getAsString method which converts an object (application specific ) to string
4-then register this converter to faces-cofig.xml
5-the used this converter in your application using tag

when apply restore view execute then it called your implemented method getAsObject() .and convert user(your) input value which is string into the specified object .
and at last before render response phases it called the getAsStrign() method and convert specific type object into the string object .




Let Start with playing Code:
Step1:Create a class and Implement the Converter interface
public class ConvertClassWhichImplement implements Converter{
  .......
..........
}

Step 2: Implement the getAsObject method 
In this method i just create object of my bean class and assign the user value in this class
  public Object getAsObject(FacesContext faces,UIComponent ui,String value){
UserClass claseuser=new UserClass();
claseuser.setUserName(value);
System.out.println("ConvertClassWhichImplement"+value);
return claseuser;
}

Step 3: Implement the getAsString method:
in this method i just convert the application object to string .
public String getAsString(FacesContext facescontext,UIComponent uicompnent,Object value){
     return value.toString();
}

Step 4: Register custom converter with faces context:
This is a very importance step and we have two choice to impement this step
1-register our converter class with id like this.and used tag to call this conveter .

<converter>
<converter-id>convertbyuser</converter-id>
<converter-class>ConvertClassWhichImplement</converter-class>
</converter>
2-second way going to minimize your work littel bits .in this way you just need to bind convert to class .and also you do not need to used the .because it automatically figur out that this class is bind with this Converter like this
<converter>
<converter-for-class>UserClass</converter-for-class>
<converter-class>ConvertClassWhichImplement</converter-class>
</converter>


Step 5: Use the converter tag in your JSP
        yes friends this is last step .which show our whole work.This is for the first part of the fourth  phases
        1-<h:inputText value="#{userName.userclassvalue}" id="user1" >
<f:converter converterId="convertbyuser"/>
</h:inputText>

      2-This is for second

</h:outputLabel>
<h:inputText value="#{userName.userclassvalue}" id="user1" >
</h:inputText>