Simple Custom Input Conversion: Failover during PROCESS_VALIDATIONS phase before Validators execute


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
	xmlns:foo="http://www.ibm.com/xsp/core/foo"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.ibm.com/xsp/core xsdxp://localhost/xsp~core.xsd">
	<xp:label value="Valid value needs to include at least a '-' or '*' character!" id="label1"></xp:label>
	<xp:br></xp:br>
	<xp:br></xp:br>
	<xp:inputText id="inputText1">
		<xp:this.converter>
			<xp:customConverter>
				<xp:this.getAsObject>
					<![CDATA[#{javascript:
						// convert the incoming submitted component value...
						// note that 'value' is a special pseudo variable available here
						// that essentially represents the components submitted value...
						if(value.indexOf("-") > -1){
							value = value.replace("-", "*");
						}else if(value.indexOf("*") > -1){
							value = value.replace("*", "-");
						}else{
							// failover in the PROCESS_VALIDATIONS phase as the value
							// could not be converted as intended...
							var msgStr = "Not a good value for conversion! Where are the '-' or '*' characters?";
							var msgObj = new javax.faces.application.FacesMessage(
							javax.faces.application.FacesMessage.SEVERITY_ERROR, msgStr, msgStr);
							facesContext.addMessage(getClientId(this.getId()), msgObj);
							this.setValid(false);
						}
					}]]>
				</xp:this.getAsObject>
				<xp:this.getAsString>
					<![CDATA[#{javascript:
						// convert the value object appropriately for rendering...
						// note that 'value' now represents the components actual value...
						try{
							return value.toString();
						}catch(e){
							// failover in the RENDER_RESPONSE phase as the value could
							// not be retrieved in string format...
							var msgStr = "Not available as not converted correctly!";
							var msgObj = new javax.faces.application.FacesMessage(
							javax.faces.application.FacesMessage.SEVERITY_ERROR, msgStr, msgStr);
							facesContext.addMessage(getClientId(this.getId()), msgObj);
							this.setValid(false);
						}
					}]]>
				</xp:this.getAsString>
			</xp:customConverter>
		</xp:this.converter>
	</xp:inputText>
	<xp:message id="message1" for="inputText1"></xp:message>
	<xp:br></xp:br>
	<xp:messages id="messages1"></xp:messages>
	<xp:br></xp:br>
	<xp:button value="Submit" id="button1">
		<xp:eventHandler event="onclick" submit="true"
			refreshMode="complete">
			<xp:this.action>
				<![CDATA[#{javascript:print("hello")}]]>
			</xp:this.action>
		</xp:eventHandler>
	</xp:button>
</xp:view>
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...