Custom Validators using JSF
The custom validators are user-defined validators that are created to meet application-specific validation. (For Standard Validators check here)

Custom Validators can be created in 2 ways

  • Implementing Validator Interface
  • Use Bean Method

Example: Lets create a JSF page to check if the person is eligible to vote using custom validators

Steps:
1. Create cvalidator java class and implement validator, write a function for validation
2. JSF Managed Bean
3. Register Custom validator class in faces-config.xml or using Annotations
4. Bind it with the UI component in JSF page

Implementing Validator Interface:
Create a java class and implement validator interface.

cvalidator.java

public class cvalidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        int bdate=(Integer)value;
        
        if(bdate<=18)
        {
            FacesMessage message=new FacesMessage();
            message.setSummary("You are Underage");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);     
        }
    }  
}

JSF Managed Bean – age.java

import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;

/**
 *
 * @author Vikram
 */
@Named(value = "age")
@Dependent
@ManagedBean
public class age {

   int cage;

    public int getCage() {
        return cage;
    }

    public void setCage(int cage) {
        this.cage = cage;
    }
    public age() {
    }
    
}

Registering Custom Validator class:
This can be done in 2 ways,

  • Register in faces-config.xml
  • using Annotations

Faces-config.xml:

<validator>
       <validator-id>cvalidator</validator-id>
       <validator-class>cvalidator</validator-class>
</validator>

Annotations:

@FacesValidator(value="cvalidator")

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Custom Validator</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputLabel value="Enter your Age"/>
            <h:inputText value="#{age.cage}">
                <c:validator validatorId="cvalidator"/>
            </h:inputText>
            
            <h:commandButton value="Validate" action="index"/>
        </h:form>
    </h:body>
</html>

Bean Method:
JSF ManagedBean – age.java

import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
@Named(value = "age")
@Dependent
@ManagedBean
public class age {

   int cage;

    public int getCage() {
        return cage;
    }

    public void setCage(int cage) {
        this.cage = cage;
    }
    public age() {
    }
 public void Validate(FacesContext fc,UIComponent c, Object value) throws ValidatorException
    {
        
        int bdate=(Integer)value;
        if(bdate<=18)
        {
            FacesMessage message=new FacesMessage();
            message.setSummary("You are Underage");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
            
        } 
    } 
    
}

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Custom Validator</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputLabel value="Enter your Age"/>
            <h:inputText value="#{age.cage}" validator=#{age.validate}"> 
            </h:inputText>
            
            <h:commandButton value="Validate" action="index"/>
        </h:form>
    </h:body>
</html>

Output:


out1

 

 

 

 

 

 

 

 

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *