Main Tutorials

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" />

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Haroldo de Oliveira Pinheiro
11 years ago

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.

Edd
10 years ago

Your solution saved me hours of headache. Thank you!

william
2 years ago

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.

william
2 years ago

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.

hichem90
7 years ago

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)

semvrul
8 years ago

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.

????????
9 years ago

Hello mr. @Mkyoung, what about skipping validation, when you chose date in tag?

Mike
10 years ago

I have the same problem as yours.

sridhar
11 years ago

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???

raul
11 years ago

process=”@this” works ok, if inmediate=”true” doesnt work.

APU
11 years ago

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.

Butchi Reddy
5 years ago
Reply to  APU

Do you have any solution for this?

tiru
13 years ago

Good solution.