Jini Samuel
Jini Samuel

Reputation: 124

Switch off Validations during ajax call

I have a JSF page in which I am iterating a list to display the <h:dataTable> with some rows containing checkbox, some text and a textbox. I have added validations such that if a chechbox in a row is checked, the user must enter value inside the corresponding textbox. Also, I have a checkbox with id 'copy' above the <h:dataTable> which should copy the value entered in the first textbox to all other textboxes whose checkboxes are checked within the <h:dataTable>.I used an ajax call for the same.

 <h:form prependId="false" id="form">
 <h:selectBooleanCheckbox value="#{bean.copy}" id="copy">
        <p:ajax process="#{bean.mailId}"
            event="change" partialSubmit="true" 
            listener="#{Controller.copyEmail()}"
            update="rm">
        </p:ajax>
    </h:selectBooleanCheckbox>

   <h:dataTable id="rm" width="100%" cellspacing="4"
        value="#{controller.alertTriggers}" var="alt"
            columnClasses="c1,c2,c3,c4">  
            <h:column>
                  <h:selectBooleanCheckbox value="#{alt.checkValue}" id="checkbox" binding="#{checkbox}"/>
               </h:column>
                <h:column>
                   <h:outputText value="#{alt.id}" />                                       
                </h:column>
                <h:column>
                  <h:outputFormat value="#{alt.msg1}" />                              
                </h:column>
                <h:column>                                              
                     <h:message for="emailID" id="email" styleClass="validation-error"/>
                     <h:inputText value="#{alt.mailId}" id="emailID" style="width: 87%;" required="#{checkbox.value}" requiredMessage="Enter the value"/>

                </h:column>                                         

  </h:dataTable>                                          
 </h:form>  

In the controller

public void copyEmail() {

//code for copying the first textbox value to all other textboxes

}

The Validation works fine, but when i check the copy checkbox, the textbox value do not get copied to the other textboxes. Instead, validation errors are shown. If I remove validations from the textbox, the copying part works fine.

I tried using immediate=true for my ajax call but then the value of the checkboxes inside the <h:dataTable> are not updated within the controller.

So, in summary

I want to switch off my validations during the ajax call. Is there any way to do that other than immediate=true?

Upvotes: 1

Views: 525

Answers (1)

BalusC
BalusC

Reputation: 1108622

In other words, you want to validate the input text field as required only when the checkbox is checked and the true save button is pressed? If so, then you'd need to extend the required attribute with another check if the save button is been pressed. You can check that by the presence of the save button's client ID in the request parameter map.

E.g.

<h:selectBooleanCheckbox binding="#{checkbox}" .../>
...
<h:inputText ... required="#{checkbox.value and not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" ... />

Upvotes: 3

Related Questions