Krishna
Krishna

Reputation: 7294

How to implement jsf validator?

HI,

I want to know how to implement Validator in JSF. What is the advantages of declaring the validator-id. When it will be called in the life cycle?. I have implemented the following code. Please find out what is wrong in the code. I am not seeing it called anywhere in the life cycle.

   <?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
 <lifecycle>
  <phase-listener>javabeat.net.jsf.JsfPhaseListener</phase-listener>
 </lifecycle>
 <validator>
  <validator-id>JsfValidator</validator-id>
  <validator-class>javabeat.net.jsf.JsfValidator</validator-class>
 </validator>

 <managed-bean>
  <managed-bean-name>jsfBean</managed-bean-name>
  <managed-bean-class>javabeat.net.beans.ManagedBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>success.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>


public class JsfValidator implements Validator {

 public JsfValidator()
 {
  System.out.println("Inside JsfValidator Constructor");
 }
 @Override
 public void validate(FacesContext facesContext, 
       UIComponent uiComponent, 
       Object object)
   throws ValidatorException {
  System.out.println("Inside Validator");
 }
}

Upvotes: 1

Views: 1757

Answers (1)

Aravind Yarram
Aravind Yarram

Reputation: 80176

You have to tie this custom validator to some jsf component on the jsf page, and that is where the vaidator-id you have defined is used. for e.g your jsf page might look something like this

<h:inputText id="myInput" value="#{mybean.property}">
     <f:validator validatorId="JsfValidator"/>
</h:inputText>

If you are learning jsf validation and conversion then I strongly recommend you to go through this article which clearly explains with examples on in which jsf life cycle phase the validation happens etc.

Upvotes: 2

Related Questions