PDA

View Full Version : commons validator and HttpServletRequest


bigal
Sep 4th, 2005, 12:21 AM
Hey,

I have a form that has a password field and a confirm password field. I would like my validation code to ensure that both these fields have the same values. Thus, if password = bob and the confirmPassword = ted, validation should fail. I would like to set this up as a common validator rule much like validWhen. Also, because I am anal, I do not want the confirm field to be a property of my bean. For instance, I do not want to have a confirmPassword property in my bean. Thus, in my validation code I would like to validate my bean against other fields in the form that are not associated with my bean. My problem is that I cannot get access to the request in my validator code because spring's validator class doesn't take it as a parameter.

My question is, what is the best way to do this? Should I just make the confirmPassword a property of my bean(I hate this it reminds my of ActionForms). Or is there an easy way to do this in spring? Has anyone else had this problem?

cuong
Sep 6th, 2005, 07:21 AM
I've just had to do this for the project I'm on and I feel your pain! I don't know if this is best way but given my limited capabilities...

- I couldn't see a way of not having a confirmPassword on my bean because the commons validator needs to work on bean properties. So, trying not to pollute my 'User' domain class, I created the subclass 'UserEditForm' and commons validator uses that instead. As far as the non-web classes (DAOs etc.) are concerned they're still 'see' a User object.

- I don't know about the current version of the Spring/Commons Validator module, but whenValid wasn't support so I wrote a custom validate method that compares property A with B only if B is not blank/empty/null.

I suppose you could override the onSubmit(request,response,command,errors) method on your FormController. I didn't go down this path because you lose the ability to configure the validation.

HTH

Cuong.

bigal
Sep 6th, 2005, 06:04 PM
Thanks for the reply,

I am glad I am not the only one with this problem, I thought I was missing something for a minute. I attempted to get it working by overriding the Validator interface and all classes that use it, to pass the request as an argument to the validate method, but it turned out to be very ugly since a lot of stuff I needed was private. Anyways, I will probably do what you suggest and make a form class.

Thanks for the help,

Al

brainmuffin
Aug 29th, 2007, 05:32 PM
Wow....two years later and you still can't do this. Most odd.