It's not a secret anymore!
The moose likes Struts and the fly likes Struts2 conversion/validation problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 conversion/validation problem" Watch "Struts2 conversion/validation problem" New topic

Struts2 conversion/validation problem

M Turner

Joined: Dec 16, 2009
Posts: 25
My model object has an XMLGregorianCalendar for a birthDate attribute. So I have created a custom converter which takes the string and converts it. Birthdate is also a required field.

My converter is global - I have it listed in the file

What's happening is that if the user types in a string which is not parseable by the MM/dd/yyyy pattern, the conversion error is thrown (great np there) HOWEVER the "required" validation error is thrown ALSO, so the screen shows up with the invalid message and the required message PLUS the birthDate field is blank.

I thought that ValidationAware was supposed to retain the user's original value? Also, I understand that the model object will not have a populated birthDate because the conversion failed, but is there a way to short-circuit the "required" validation if a conversion error occurs.

P.S I tried adding the following to the action-validation.xml file - in order to save the user's original entry :

However, the value isn't repopulated and now the "invalid" error is shown twice. I also tried adding "short-circuit="true" to the field-validator, but it didn't short-circuit the "required" logic.

Mahendr Shinde
Ranch Hand

Joined: Sep 03, 2011
Posts: 38

Your problem might be because of "ConversionError" Interceptor who raises an Exception and automatically adds "Conversion" error to your field.
Plus, you have got your own "conversion" field validation. Your another problem is that, You have to "short-circuit" your first "required" validator.

There is still lot to learn!
I agree. Here's the link:
subject: Struts2 conversion/validation problem
jQuery in Action, 3rd edition