Advanced Custom Input Validation: Failover during PROCESS_VALIDATIONS phase


Code 1 - FooValidator.java

package com.ibm.xsp.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/*
 * @author Tony McGuckin, IBM
 */
public class FooValidator implements Validator {
	
	public FooValidator(){}
	
	//------------------------------------------------------------------------
	
	public void validate(FacesContext context, UIComponent component, Object value)
	throws ValidatorException {	
		String email = (String)value;
		Pattern p = Pattern.compile(".+@foo.com");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();
		if (!matchFound) {
			String msgSummary = "invalid 'foo' email format!";
			String msgDetail = email + " is not a valid 'foo' email format!";
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msgSummary, msgDetail);
			throw new ValidatorException(message);
		}
	}
	
} // end FooValidator


Code 2 - faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
	<validator>
		<validator-id>fooValidator</validator-id>
		<validator-class>com.ibm.xsp.validators.FooValidator
		</validator-class>
	</validator>
</faces-config>


Code 3 - XSP Markup

<xp:inputText id="inputText4">
	<xp:this.validators>
		<xp:validator validatorId="fooValidator"></xp:validator>
	</xp:this.validators>
</xp:inputText>
<xp:message id="message4" for="inputText4"></xp:message>
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...