Two Laptop Bag*
The moose likes Struts and the fly likes Struts2: use EmailValidator in my validate() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: use EmailValidator in my validate() method?" Watch "Struts2: use EmailValidator in my validate() method?" New topic
Author

Struts2: use EmailValidator in my validate() method?

M Turner
Greenhorn

Joined: Dec 16, 2009
Posts: 25
Can I still leverage the type validators in my validate() method if I'm not using an xml file? I need to override validate() for my form validation, but still want to leverage some of the type validators from the framework. Is there an example of how to use the type validators without the xml? In Struts 1x, I would have used GenericTypeValidator - do I have to add Commons Validation to my project on top of the XWorks Validation to get type validators?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use annotations, or use both the XML and validate() method, or validate everything manually.
M Turner
Greenhorn

Joined: Dec 16, 2009
Posts: 25
Right. So if I want to validate everything in validate(), can I still leverage the Xwork type validators?

OR

Can I switch the ordering of the validation calls such that the validate() method is called first .. then the xml?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I guess I don't see why you *couldn't* leverage the XWork validations manually, but I also don't understand why you wouldn't just use the annotations. I don't think I've ever used them on their own.

It's unclear to me why you'd want to run the validate() method before the XML--XML validation is for the easy stuff, like "does this field even exist" etc. Anything more complex, which is what should go in the validate() method, would depend on the XML validation anyway, at least in every use case I've ever had. You might be able to play with the order of the interceptors; off the top of my head I honestly don't remember what the default order is or which interceptor does which part of the work--you might have to poke around in the source a bit if nobody else chimes in. Worst case scenario would be a relatively minor tweak to an existing interceptor.
M Turner
Greenhorn

Joined: Dec 16, 2009
Posts: 25
Thanks for your thoughts Dave. My mind also went to reordering interceptors but then I knew I was in too deep...

Here's what I'm really getting at... I need to have only one error message for 3 fields - but I want to also test for whitespace which is build into requiredstring.. however that is a FieldValidator. So I tried the following:



which doesn't work and gives an error in my logs:

com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn Got result of null when trying to get Boolean.

I guess my real question is.. is there a nice way to test for whitespce in OGNL?


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If I were doing it I'd try either using the complete OGNL static syntax, or just write a custom validator--they're quite easy.
M Turner
Greenhorn

Joined: Dec 16, 2009
Posts: 25
Hmm - OhGoshNL huh.. well I'm definitely new to OGNL, but the following gave the same error as above:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2: use EmailValidator in my validate() method?