10

I have page in which I edit some entity. That page has two command buttons. One is "Back" and one is "Save" and also on that page I have form with input fields (idInputSubject). Some of them are required, some are not.

How can I ensure that we I press "Back" button (cancel editing and go back) validation will be ignored, which is not the case now. Now, when I press either "Back" or "Save" button validation's messages appear if I did't fill required filed with the correct values (idInputSubject).

Both "Back" and "Save" buttons are in the same form:

<h:form id="idFormMeasureDetail" styleClass="bodyForm" prependId="false">
   ...
   <p:commandButton value="#{contentMB.msg.label_back.value}"
        action="#{chooseMeasureControllerMB.aSearch}"
        rendered="#{detailMeasureMB.navigation eq 0}" ajax="false"
        icon="ui-icon-arrowreturnthick-1-w"/>
   <p:commandButton value="#{contentMB.msg.button_save.value}" ajax="false" 
        icon="ui-icon-disk" actionListener="#{detailMeasureControllerMB.alApplyChanges}" 
                    title="#{contentMB.msg.tip_Apply.value}" />

   ...
   <p:inputTextarea id="idInputSubject" value="#{detailMeasureMB.measure.aufgabe}"
    readonly="#{!userSessionMB.supervisor and !detailMeasureMB.isCreator}"
    required="#{globalSessionMB.globalWebOptionsMap['MMRequiredSubject'].propvalue}"
            title="#{contentMB.msg.tip_Betreff.value}"
            autoResize="false" style="width:100%;" >
   </p:inputTextarea>
   <p:message id="inputSubjectMsg" for="idInputSubject" display="icon" />
   ...

</h:form>

ChooseMeasureControllerMB:

@ManagedBean(name = "chooseMeasureControllerMB")
@RequestScoped
public class ChooseMeasureControllerMB extends BaseMeasureControllerMB {
...
public String aSearch() {
            ...
    // navigate to target-page
    return "/pages/mm/showMeasuresList.xhtml?faces-redirect=true";
}
...
}
Nikola
  • 624
  • 1
  • 14
  • 31

2 Answers2

17

If you want prevent/skip validation on certain button click use

immediate="true" on that specific button

For a good explanation about the immediate="true" read the following BalusC answer

And here a nice diagram that shows how imemdiate="true works"

Community
  • 1
  • 1
Daniel
  • 36,833
  • 10
  • 119
  • 200
0

In short, when you're clicking the "back" button, you submit the form and the data is validated. To prevent this, place the button in another form or use Java-script for the navigation.

In general, when you want to cancel an operation you don't want to submit the data, since it is to be discarded. Use some client side action. I think you can also use a <p:commandButton> or a <p:button> for navigation. This way the form isn't submitted either.

siebz0r
  • 18,867
  • 14
  • 64
  • 107