*
The moose likes Struts and the fly likes Struts2 validation localization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 validation localization problem" Watch "Struts2 validation localization problem" New topic
Author

Struts2 validation localization problem

Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
Ok i have a problem with localization and the error output.

lets say i have a message defined like this

and a validator like this

if this fails Im going to get an error message like

"requestDate is required"

but what I want is

"Request Date is required".

in struts1 using the commons validator I was able to define a parameter which would be used for display purposes on error messages...how can i achieve that in struts2 validation framework??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

All OGNL is available in resource strings, allowing all sorts of madness. If I remember I'll cut-and-paste an example from the book where I cover this.
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
thanks david, unless you can suggest a better solution i came up with this idea to just do something like this



and just rinse and repeat for all my fields. i dont particularly like this method...i dont know why but its looks messy but i gotta work with what i have.


EDIT: or better yet maybe something like this...i like this a little better
EDIT#2: Below DOES NOT WORK, but leaving it here for reference!!


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Doesn't that mean you're defining the label in two places though?
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
im not sure what you are referring too david. But either way...the second option was a no go as the validator does not except params that are not defined in that type of validator. so i was not able to create a custom "label" param.

as for my first solution if your referring to the fact I define a string
form.requestDate.label = Request Date

and then call that string like this
<message>${getText("form.requestDate.label")} ${getText("required")}</message>

then i suppose it could be defining it twice since i might as well just explicitly say "Request Date" in the <message>, but the fact is that there are many instances in the app where the same field is used throughout many different forms. so defining it once makes it reusable no matter what form or page and its easy just to update the label field once in case i need too instead of searching through multiple validation files to update that field.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I was talking about the second one (which I didn't actually think would work, but misinterpreted your message as meaning it did).
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
yea that was my bad, i know that its struts1 you could arbitrarily define any params you want and i assumed you could do the same in struts2 so i suggested #2 w/out even testing it. wont make that mistake again!! ill edit my post to clarify it doesn't work!!
mehdi chorsi
Greenhorn

Joined: Jun 05, 2013
Posts: 25
Hi Raymond Holguin

Did you solve the problem ?

I have same problem now could you please help me?

a sample or a link
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
For me i just stuck with my orignal solution



This allowed me some flexibility for the scenarios where I use the same label multiple times throughout my app then its standardized and defined in one location and all i have to do is just reference the label string. And if i want to change how "Request Date" is displayed i only have to change it in one location instead of several.
mehdi chorsi
Greenhorn

Joined: Jun 05, 2013
Posts: 25
Ok thank you so much it is good solution

I've done and It's work
 
jQuery in Action, 2nd edition
 
subject: Struts2 validation localization problem
 
Similar Threads
Struts2 conversion/validation problem
Struts2 validation
Struts2 & validation [SOLVED]
Validation Struts2
Conditional validation in Struts2