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

No comments:

Post a Comment