You can also take advantage of the fact that the required attribute accepts EL expressions, like shown here.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="
http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="
http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>
<f:view>
<h:form>
<h:messages />
<h:panelGrid columns="3">
<h:outputText value="Check" />
<h:selectBooleanCheckbox value="#{peopleBean.someBool}" id="someBool" />
<h:message for="someBool" />
<h:outputText value="Optional" />
<h:inputText value="#{peopleBean.someText}" id="someText" required="#{view.children[0].children[1].children[1].value}" />
<h:message for="someText" />
<f:facet name="footer">
<h:commandButton value="Click" />
</f:facet>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
I know it looks crappy, but there are plenty of options to make it nicer. For example, with Facelets you can create a custom function to lookup a component by id.
Functions.java:
public class Functions {
public static UIComponent find(String id) {
FacesContext fc = FacesContext.getCurrentInstance();
return fc.getViewRoot().findComponent(id);
}
}
functions.taglib.xml:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "
http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>
http://www.ordina.nl/facelets/functions</namespace>
<function>
<function-name>find</function-name>
<function-class>nl.ordina.facelets.functions.Functions</function-class>
<function-signature>javax.faces.component.UIComponent find(java.lang.String)</function-signature>
</function>
</facelet-taglib>
web.xml:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/functions.taglib.xml</param-value>
</context-param>
And finally, your web page:
<!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:ui="
http://java.sun.com/jsf/facelets"
xmlns:f="
http://java.sun.com/jsf/core"
xmlns:h="
http://java.sun.com/jsf/html"
xmlns:o="
http://www.ordina.nl/facelets/functions">
<head></head>
<body>
<h1>Test</h1>
<h:form id="form">
<h:messages />
<h:panelGrid columns="3">
<h:outputText value="Check" />
<h:selectBooleanCheckbox value="#{peopleBean.someBool}" id="someBool" />
<h:message for="someBool" />
<h:outputText value="Optional" />
<h:inputText value="#{peopleBean.someText}" id="someText"
required="#{o:find('form:someBool').value}" />
<h:message for="someText" />
<f:facet name="footer">
<h:commandButton value="Click" />
</f:facet>
</h:panelGrid>
</h:form>
</body>
</html>
[Message sent by forum member 'jkva' (jkva)]
http://forums.java.net/jive/thread.jspa?messageID=294989