Developing Web 2.0 applications
with JavaServerFaces
and Dreamweaver
>> Just Became Easier
Documentation
Search
  1. Introduction
  2. Getting Started
  3. Developing Web Sites
  4. Using Components
  5. Internationalization
  6. Code View Support
  7. Design View Support
  8. Keyboard Shortcuts
  9. Tag Object Toolbars
  10. JSF Server Behaviors
  11. Property Inspectors
  12. JSF Tag Reference
JSF Core Tag Library
  f:actionListener
  f:ajax
  f:attribute
  f:convertDateTime
  f:convertNumber
  f:converter
  f:event
  f:facet
  f:loadBundle
  f:metadata
  f:param
  f:phaseListener
  f:selectItem
  f:selectItems
  f:setPropertyActionListener
  f:subview
  f:validateBean
  f:validateDoubleRange
  f:validateLength
  f:validateLongRange
  f:validateRegex
  f:validateRequired
Selected f:validator
  f:valueChangeListener
  f:verbatim
  f:view
  f:viewParam
If you like this documentation, please take a look at my book:

12. JSF Core Tag Reference

Documentation > JSF > JSF Tag Reference > JSF Core Tag Library > f:validator

f:validator

The Validator tag registers a named Validator instance on the component associated with the enclosing tag. The JavaServer Faces framework includes three standard validators (see the validateDoubleRange, validateLength, and validateLongRange tags) but the Validator interface can be implemented by classes that provide custom validation for your application. This tag accepts one value matching the validator ID you assigned to your validator class in your Faces configuration file. The body content of this tag must be empty.

JSF Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
<h:form>
	<h:outputLabel value="Enter your email address: " for="emailAddress" />
	<h:inputText label="Email address" id="emailAddress" required="true" 
	             value="#{customerController.customer.emailAddress}">
		<f:validator validatorId="emailAddressValidator" />
	</h:inputText>
	<h:commandButton value="Submit" />
	<h:message for="emailAddress" errorStyle="color:red; display:block" />
</h:form>
</body>
</html>
This example was formatted by JSFToolbox for Dreamweaver.

Java Code

package com.mycompany.validate;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * Simple email validator to demonstrate custom validation.
 * 
 */
@FacesValidator(value = "emailAddressValidator")
public class EmailAddressValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		String email = String.valueOf(value);
		boolean valid = true;
		if (value == null) {
			valid = false;
		} else if (!email.contains("@")) {
			valid = false;
		} else if (!email.contains(".")) {
			valid = false;
		} else if (email.contains(" ")) {
			valid = false;
		}
		if (!valid) {
			FacesMessage message = new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Invalid email address",
					"The email address you entered is not valid.");
			throw new ValidatorException(message);
		}
	}
}

Rendered Output

Rendered f:validator tag

Tag Attributes

binding Expression
 
The value-binding expression linking this component tag to a backing bean property.
disabled Boolean
 
The disabled attribute is a boolean flag that when set to true indicates that this component should not receive focus or be included in a form submit.
for Text
 
If present, this attribute refers to the value of one of the exposed attached objects within the composite component inside of which this tag is nested.
validatorId Text
Required
The validatorId attribute sets the validator identifier of the Validator to be created and registered. It must match exactly the value of one of the validator-id element of a validator in your Faces configuration file.