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 the Author

author image
mkyong
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

avatar
10 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
11 Comment authors
Butchi Reddyhichem90semvrul????????Edd Recent comment authors
newest oldest most voted
Edd
Guest
Edd

Your solution saved me hours of headache. Thank you!

Haroldo de Oliveira Pinheiro
Guest
Haroldo de Oliveira Pinheiro

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.

hichem90
Guest
hichem90

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
Guest
semvrul

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.

????????
Guest
????????

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

Mike
Guest
Mike

I have the same problem as yours.

sridhar
Guest
sridhar

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
Guest
raul

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

APU
Guest
APU

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
Guest
Butchi Reddy

Do you have any solution for this?

tiru
Guest
tiru

Good solution.