This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Custom Validations in Struts Validator Framework? 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 "Custom Validations in Struts Validator Framework?" Watch "Custom Validations in Struts Validator Framework?" New topic
Author

Custom Validations in Struts Validator Framework?

M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
The function defined in the custom validation class gets called, but somehow i am not getting the error message when i voilate the validation.

Validation.xml
============================


validator-rules.xml
============================



SalesRepExistanceValidator class (my custom validation)
=======================================================


Keys defined in the property file
==================================


Kindly help !!!
[ November 16, 2006: Message edited by: Manish Sachdev ]

Regards, Manish
SCJP 1.4
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Change

to

The string argument for the ActionMessage constructor should be a message key, not the actual text of the message.


Merrill
Consultant, Sima Solutions
sridhar ravi
Greenhorn

Joined: Aug 24, 2006
Posts: 3
Hi,

Have you checked in struts-config.xml
<action-mapping>
<action> the attribute should specify validate is true
and i want to know how declared onSubmit in jsp/html.

Thanks & Regards
Ravi.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
It sounds a lot like the issue that Ja vardhan is asking about (in the three active threads). Is errors coming in null? If so then I cannot imagine that the line of code that creates a new ActionErrors object is going to work. The calling method will never see the object that you created (that is just the way that parameter work in Java).

What version of Struts are you using?

- Brent
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I would be tempted to switch over to using ActionMessages. I took a look at the code for the "required" validation and here is the code for Strut 1.3:


- Brent
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Thanks for your valuable inputs folks!!!

Brent you pointed it right, i receive errors object as null in this method. I think ideally it should be the one which is present in request.

Apart from this custom validation, all other standard validations are working.

The version of struts being used is 1.2.9

I have made some changes, which are marked in bold in the code below:

My Validator Class i.e. SalesRepExistanceValidator
==================================================



Output on the console as per the SOPs is
=========================================
folder matched is >> 101 (Alan Rego)
errors object is >> null
field key is >> empNumber

As we can see, the error object received in validateSalesRepExistance method is null.

Kindly help, if you have any clues on how to get this working. I am trying this from yesterday, but not able to get it.

Thanks in Advance...
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Thanks folks!!

At last i got the solution

It seems that later versions of struts, have started populating ActionMessages instead of ActionErrors in case of any validation being voilated. I just replaced the ActionErrors parameter in my validate method to ActionMessages and it worked

I have highlighted the changes in bold.

validation-rules.xml
=======================================




SalesRepExistanceValidator class (Changes done in signature only, ActionErrors replaced by ActionMessages)
=======================================================



I hope it helps others facing a similar problem
[ November 17, 2006: Message edited by: Manish Sachdev ]
 
Consider Paul's rocket mass heater.
 
subject: Custom Validations in Struts Validator Framework?
 
Similar Threads
validator plugin and validate() method needs to use together
How Do You Use Struts Validator For Non Required Field?
Having problem with struts validator for error message
NullPointer Exception
custom validator and custom tags