Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Validations in Struts Validator Framework?

 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change

to

The string argument for the ActionMessage constructor should be a message key, not the actual text of the message.
 
sridhar ravi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic