Reputation: 103
I have a jsf page with
<h:inputText required="true">
attribute.
When I click on my add button, I want to see an error if the field is blank.
I have a <f:message>
to display this.
But how can I escape (cancel) the page with that field blank? In my case it continually posts the error message and I have to enter something in the field to go to another page.
Upvotes: 2
Views: 4055
Reputation: 3072
If your cancel button is a POST requests e.g. <p:commandButton>
,<h:commandButton>
, <p:commandLink>
, or <h:commandLink>
You can put your cancel button outside the form that has the <h:inputText>
so that form is not submitted
<h:form>
<h:inputText value="#{bean.string}" required="true"/>
</h:form>
<h:form>
<p:commandButton value="Cancel" action="anotherpage.xhtml"/>
</h:form>
If you are navigating without any actions or method invocation, then you can use GET requests e.g. <h:button>
, <p:button>
, <h:link>
or <h:outputLink>
.
Example:
<h:form>
<h:inputText value="#{bean.string}" required="true"/>
<p:button value="Cancel" outcome="anotherpage.xhtml"/>
</h:form>
Upvotes: 4
Reputation: 1108742
Just don't process the form on submit if you want to cancel or navigate away.
I.e. don't do
<h:form>
...
<p:commandButton value="Cancel" action="#{bean.cancel}" />
</h:form>
But just do
<h:form>
...
<p:commandButton value="Cancel" action="#{bean.cancel}" process="@this" />
</h:form>
Or, better if you're only returning a navigation outcome in cancel()
method anyway
<h:form>
...
<p:button value="Cancel" outcome="otherpage.xhtml" />
</h:form>
Upvotes: 1
Reputation: 1905
You can use immediate="true"
in your <p:commandButton>
. Then it will skip the validation.
For example:
<p:commandButton action="#{managedBean.action}" value="Cancel" immediate="true" />
Upvotes: 2