3

I have a datatable with rows containing some input text fields that are required. Each row also has a check box called delete. I want to have the required = "true" only when the check box is selected. How can I achieve this?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
SDP
  • 63
  • 3
  • 8

1 Answers1

8

Just let the input's required attribute check the checkbox's value.

Here's a kickoff example:

<h:form>
    <h:dataTable value="#{bean.list}" var="item">
        <h:column><h:selectBooleanCheckbox binding="#{checkbox}" /></h:column>
        <h:column><h:inputText id="input" value="#{item.value}" required="#{checkbox.value == 'true'}" /></h:column>
        <h:column><h:message for="input" /></h:column>
    </h:dataTable>
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 3
    Seriously, whatever question I search for and is related to JSF. So far, everywhere @BalusC response is just perfect. Respect for you sir. :-) – Mukul Goel Nov 21 '12 at 18:39
  • a question : in the above code : Dont we need to associate a Listener on value change of checkbox to update the required property of the inputText? – Mukul Goel Nov 21 '12 at 19:53
  • @Mukul. You're welcome. No, you don't need a change listener or so. The `required` attribute is evaluated during the request of processing the form submit, not during the request of displaying the form. The example in the answer is complete as-is. – BalusC Nov 22 '12 at 03:37
  • Ahaaan... And the above example is client side authentication ? I mean the `binding` can be used like this aswell..it need not just be a bean property always? – Mukul Goel Nov 22 '12 at 07:11
  • @Mukul: No, it still runs in server side. No, you don't need a bean property if you don't need to have access to the component in some method of the bean. As to the --for starters confusing-- way of using `binding` here, check this question: http://stackoverflow.com/questions/8168302/jsf-component-binding-without-bean-property – BalusC Nov 22 '12 at 11:13