aspose file tools*
The moose likes JSF and the fly likes validation of more than one field (e.g. password verification) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "validation of more than one field (e.g. password verification)" Watch "validation of more than one field (e.g. password verification)" New topic
Author

validation of more than one field (e.g. password verification)

Ronald Johnson
Greenhorn

Joined: Jan 15, 2007
Posts: 18
I want to have a page where a user can change his password
e.g. enter the password in one field, and then he has to enter the password again in a second field to make sure that he typed his password correctly.

So, my validation would be a simple comparision that the entries are identical.

I have a backing bean for the values of the fields.

What I want is when the user clicks a button I would simply like to call a verification method in the backing bean that compares the attributes and returns some error if they are not identical.

How can i achieve this? The commandbutton does not have a validator attribute, and calling a method over the action attribute does not seem to work.

All I can find in the internet are simple examples where the value of one field is checked, not that several fields are compared with one validation method.

cheers,

RJ
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Hi Ronald,

Originally posted by Ronald Johnson:
I want to have a page where a user can change his password
e.g. enter the password in one field, and then he has to enter the password again in a second field to make sure that he typed his password correctly.

So, my validation would be a simple comparision that the entries are identical.

I have a backing bean for the values of the fields.

What I want is when the user clicks a button I would simply like to call a verification method in the backing bean that compares the attributes and returns some error if they are not identical.

How can i achieve this? The commandbutton does not have a validator attribute, and calling a method over the action attribute does not seem to work.

All I can find in the internet are simple examples where the value of one field is checked, not that several fields are compared with one validation method.

cheers,

RJ

-- What you could do is to create another field for your error message at the backing bean. In the validation method verify everithing and if anything is wrong fill your errorMessage field so JSF could render it when returning to the client. An example:

<@ taglibs.... @>
<f:view>
<h:form>
.... your tags
<hutputText value="#{yourBean.errorField}"/>
</h:form>
</f:view>

And you know the code of your bean to validate your fields.

I hope this could help.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
You should be able to compare the fields in your backing bean's action method itself. By the time the action method is called, the password and the verify password fields will already be posted to the backing bean and they will be available for your validation.
After comparing, if it fails your validation, you may choose to redisplay the same page with a message:

Make sure you are using h:messages in order to display this message.

I think there is also another approach to do a combined field validation. In this approach, you should have a dummy hidden field AFTER all your fields that need combined validation. This hidden field should have a validator attribute which will call the validation method of your backing bean.
Ronald Johnson
Greenhorn

Joined: Jan 15, 2007
Posts: 18
ok, my solution was this:



I also found out that in Apache Tomahawk (from version 1.0.5) there exist a tag t:validateEqual that does exactly what i want.

cheers!
 
Don't get me started about those stupid light bulbs.
 
subject: validation of more than one field (e.g. password verification)