How to skip validation in JSF
Problem
See following JSF example :
<h:inputSecret id="password" value="#{user.password}"
size="20" required="true" label="Password">
<f:validateRegex pattern="((?=.*\d).{6,20})" />
</h:inputSecret>
<h:message for="password" style="color:red" />
<h:commandButton value="Cancel" action="cancel" />
<h:commandButton value="Submit" action="result" />
If you click on the “cancel” button, the password validation will be fired and stop you proceed on the cancel page, which is doesn’t make sense. Is there any way to bypassing the validation in JSF?
Solution
To skip validation, add a immediate=”true” attribute to the cancel button.
<h:inputSecret id="password" value="#{user.password}"
size="20" required="true" label="Password">
</h:inputSecret>
<h:message for="password" style="color:red" />
<h:commandButton value="Cancel" immediate="true" action="cancel" />
<h:commandButton value="Submit" action="result" />
That works okay if you just want to navigate to another page; unfortunately, using ‘immediate=”true”‘ also means that whatever changes you make to the model will also be ignored.
In the example above, if you tried to make your cancel button call a method on the backing bean that would display an error message, cleanup the password field and return to the same screen, it simply wouldn’t work, since the changes would be ignored.
Your solution saved me hours of headache. Thank you!
This does not work when the field is a date and a date converter is specified in JSP associated with the input field. The date converter will throw the exception and thus blocking any attempt to escape the field validation.
I had the immediate attribute in my commandButton (in PrimeFaces 5.3) set to true but it still called the field validator. This attribute setting does not seem to have any effect.
i want help if someone could tell me how to disable one of two input fields when entring value in one of them(i’m using jsf 2.2 edition)
hello..
i have save as draft in my application, which is skip every validation including required validation but i want to get value from every component like p:inputText, p:calendar,etc.
problem is, when im using immediate=”true” the validation is skipped, but value from p:inputText always null.
there is any solution from my problem?
sorry for my bad grammar.
thanks.
Hello mr. @Mkyoung, what about skipping validation, when you chose date in tag?
I have the same problem as yours.
I’am doing an online shopping project..i need to use image zooming for the products..How can i achieve this with the help of ajax???
process=”@this” works ok, if inmediate=”true” doesnt work.
When I am using this immediate=”true” then my reset action is not working.I am just re-newing the object in the reset function like:
myObject= new myModelClass();
Can you please fix this problem.
Do you have any solution for this?
Good solution.