0

I am using JSF 2.2 with Primefaces 4 on a Tomcat 7.
I have a form in which certains fields are required depending on the value of a checkbox. So In the example below, the fields in the fieldset are required only if the checkbox is checked (which is, by default) :

<h:form id="formId">
    ...
    <p:selectBooleanCheckbox id="fieldId1" value="#{myBean.firm}">
        <p:ajax event="change" process="@this :formId:fieldsetId" update="@this :formId:fieldsetId"/>
    </p:selectBooleanCheckbox>
    ...
    <p:fieldset id="fieldsetId">
        <p:inputText id="field2" value="#{myBean.field2Value}" required="#{myBean.firm}"/>
        <p:inputText id="field3" value="#{myBean.field3Value}" required="#{myBean.firm}"/>
    </p:fieldset>
</h:form>

My problem is that the ajax request passes through the validation, so when i unchecked the checkbox, the validation fails on the inputFields if they are not filled, and the model is not updated.
So my attribute firm is still set to true in myBean and my inputText fields are still required.

At first I forgot to process the fieldset

<p:ajax event="change" process="@this" update="@this :formId:fieldsetId"/>

This worked, or I though, but as the field are not processed, if I filled some fields before unchecking the box, all the data were lost. Setting immediate="true" won't work either in that case.

It seems to me to be a very simple and common usecase, and I can't believe there is not a solution.

Thanks

Pif
  • 13
  • 6
  • Thank you @BalusC, that did the trick ! A big thanks also for all your great replies and tutorials ! – Pif Jun 26 '14 at 07:16

1 Answers1

0

Hy,

<h:form id="formId">
        ...
        <p:selectBooleanCheckbox id="fieldId1" value="#{myBean.firm}">
            <f:ajax listener="#{bean.checkBoxListener}" immediate="true" update=":formId:fieldsetId" />
        </p:selectBooleanCheckbox>
        ...
        <p:fieldset id="fieldsetId">
            <p:inputText id="field2" value="#{myBean.field2Value}" required="#{myBean.firm}"/>
            <p:inputText id="field3" value="#{myBean.field3Value}" required="#{myBean.firm}"/>
        </p:fieldset>
</h:form>

in the bean

public void checkBoxListener(ValueChangeEvent event) {
    this.firm = (Boolean) ((UIInput) event.getComponent()).getValue();
}
sghaier ali
  • 433
  • 2
  • 15
  • If you can't write code off top of head, please actually run and test it before doing it off as an answer. – BalusC Jun 25 '14 at 11:02