Validation in XPages: one of the two fields should be filled in

Recently friend of mine asked me for help with an XPages application and one of the "problems" we had was validating user inputs.
We needed to make sure that user enters something in one of the two fields. My first thought was "ok, it's going to be a simple xp:validateExpression checking for non empty string and returning true". But after giving it one more thought, I ended up with "just make one of the fields required, if the other one is empty"

In code it could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
	xmlns:xp="http://www.ibm.com/xsp/core">
	<xp:table>
		<xp:tr>
			<xp:td>
				<xp:label
					value="inputText1:"
					id="label1">
				</xp:label>
			</xp:td>
			<xp:td>
				<xp:inputText
					id="inputText1">
				</xp:inputText>
			</xp:td>
			<xp:td>
				<xp:message
					id="message1"
					for="inputText1">
				</xp:message>
			</xp:td>
		</xp:tr>
		<xp:tr>
			<xp:td>
				<xp:label
					value="inputText2:"
					id="label2">
				</xp:label>
			</xp:td>
			<xp:td>
				<xp:inputText
					id="inputText2">
					<xp:this.required><![CDATA[#{javascript:if (getComponent("inputText1").getValue()=='') {
	return true;
}}]]></xp:this.required>
					<xp:this.validators>
						<xp:validateRequired message="Enter text in one of the fields, please."></xp:validateRequired>
					</xp:this.validators></xp:inputText>
			</xp:td>
			<xp:td>
				<xp:message
					id="message2"
					for="inputText2">
				</xp:message>
			</xp:td>
		</xp:tr>
		<xp:tr>
			<xp:td
				colspan="3">
				<xp:button
					value="Submit"
					id="button1">
					<xp:eventHandler
						event="onclick"
						submit="true"
						refreshMode="complete"
						immediate="false"
						id="eventHandler1">
					</xp:eventHandler>
				</xp:button>
			</xp:td>
		</xp:tr>
	</xp:table>
</xp:view>

Tags: