Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ronald Johnson
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic