Advanced Custom Input Conversion: Failover during PROCESS_VALIDATIONS phase before Validators run


Code 1 - FooConverter.java:

package com.ibm.xsp.converters;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/*
 * @author Tony McGuckin, IBM
 */
public class FooConverter implements Converter{

	public FooConverter(){}
	
	//------------------------------------------------------------------------
	
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value.indexOf("-") > -1){
			value = value.replace("-", "*");
			return value;
		}else if(value.indexOf("*") > -1){
			value = value.replace("*", "-");
			return value;
		}else{
			String msg = "Not a good value for conversion! Where are the '-' or '*' characters?";
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
			throw new ConverterException(message);
		}
	}
	
	//------------------------------------------------------------------------

	public String getAsString(FacesContext context, UIComponent component, Object value) {
		try{
			return value.toString();
		}catch(Exception e){
			String msg = "Not available as not converted correctly!";
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
			throw new ConverterException(message);
		}
	}
	
} // end FooConverter


Code 2 - faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <converter>
    <converter-id>fooConverter</converter-id>
    <converter-class>com.ibm.xsp.converters.FooConverter</converter-class>
  </converter>
</faces-config>


Code 3 - XSP Markup:

<xp:inputText id="inputText2">
	<xp:this.converter>
		<xp:converter converterId="fooConverter"></xp:converter>
	</xp:this.converter>
</xp:inputText>

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...