JSF Custom Validator with Example




Hey guys in this post, we will discuss adding custom validator to JSF form with Example

Requirement


We need to validate the Postal Code. It should start with IND followed the by the postal code, otherwise we need to show the validation message.

Complete example


We will create this example step by step, follow this tutorial till the end

Read More:

Create Dynamic Web Project


To create a dynamic web project, you can read the following tutorial

Create a bean


Create Register.java file inside the in.bushansirgur.jsf.beans package and add the following content

package in.bushansirgur.jsf.beans;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

@ManagedBean
public class Register {
	
	private String email;
	
	private String name;
	
	private String postalCode;
	
	public Register() {
	
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}
	
	public void validatePostalCode (FacesContext context, UIComponent component, Object value) throws ValidatorException {
		
		if (value == null) {
			return;
		}
		
		String data = value.toString();
		
		if (!data.startsWith("IND")) {
			FacesMessage message = new FacesMessage("Postal code must start with IND");
			throw new ValidatorException(message);
		}
	}
}

We will create a new method validatePostalCode() which takes 3 parameters, FacesContext, UIComponent, and the Object, which is the actual value entered in the textbox. Inside this method we will write the business logic to validate the Postal code.

Create Input form


Create register-form.xhtml file inside the WebContent and add the following content

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
		xmlns:h="http://xmlns.jcp.org/jsf/html"
		xmlns:c="http://xmlns.jcp.org/jsf/core">

	<h:head>
		<title>Input Form</title>
		<style>
			.error {
				color: red;
				font-style: italic;
			}
		</style>
	</h:head>
	
	<h:body>
		
		<h:form>
			Name: <h:inputText id="name" label="Name" value="#{register.name}">
				<c:validateLength minimum="5" maximum="15" />
			</h:inputText>
			<h:message for="name" styleClass="error" />
			<br/><br/>
			Email: <h:inputText id="email" value="#{register.email}" validatorMessage="Please enter valid email">
				<c:validateRegex pattern="^[A-Za-z0-9+_.-][email protected](.+)$" />
			</h:inputText>
			<h:message for="email" styleClass="error" />
			<br/><br/>
			Postcal Code:
			<h:inputText id="postal_code" value="#{register.postalCode}" validator="#{register.validatePostalCode}" />
			<h:message for="postal_code" styleClass="error" />
			<br/><br/>
			<h:commandButton value="Register" action="dashboard" />
		</h:form>
		
	</h:body>

</html>

We will create a new text box for postal code. We will add a new attribute validator, it takes the validator method validatePostalCode() which is defined in the managed bean Register

Create a Response page


Create dashboard.xhtml file inside the WebContent and add the following content

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
		xmlns:h="http://xmlns.jcp.org/jsf/html"
		xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

	<h:head>
		<title>Dashboard</title>
	</h:head>
	
	<h:body>
		
		<div>
			<span>The Logged in user is: #{register.email}</span>
		</div>
		<div>
			<p>Your name is: #{register.name}</p>
		</div>
	</h:body>

</html>

Run the app


To run the application, right click on the project, select Run As -> choose Run on Server

Open the browser, navigate to the URL http://localhost:8080/JSF-hello-world/faces/register-form.xhtml

14

That’s it for this post. If you like this post, then please share this with your friends and collogues. Also share this post on your social media profiles as well.



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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