wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts validation" Watch "Struts validation" New topic
Author

Struts validation

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi again all. I've started on another part of struts that I am hoping to get more excellent help as I have in the past here. I want to place validation into my struts application. The part that has me confused is that I only want to place validation on certain pages on certain points. As an example I have one form that is used by several pages, but I only want the validation to happen when 1 particular action is called. I am guessing this isn't a problem, as one simply has to put validation="true" in the required action mapping. This is only a small part of the problem. The larger part of the problem for me is to write the validation.xml for the form.

The part that I am confused about is that the variable I want to confirm (a string) is contained in an object which is in an arraylist. I want to confirm that each object only has numbers in the string.

Would someone be able to help by pointing out how one iterates over an arraylist to verify each individual object ?

thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm afraid the news is not good. The Struts Validation Framework doesn't support validation of indexed properties.

There is a sort of work-around. If you expect the number of elements to always be within a certain range, you can specify a validation for each element. Example:

For me, though, this workaround is unacceptable. If The list has 10 elements, and I've only defined 9 in the xml file, the 10th one doesn't get validated. If I have list, I want each element to be validated regardless of how many there are. My suggestion would be to use the validation framework for everything else, but in this particular case, just override the validate() method of your ActionForm and put your validation logic there.


Merrill
Consultant, Sima Solutions
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
I was afraid of that. Unfortunately My lists may be of indeterminant size, most likely under 50 but that cannot be guarenteed. I will try to use what you have suggested, but may be back for further assistance.

as always thanks again for the great pointers.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
I'm back. I am curious about something. I am trying to use the example that I found here :
http://www.roseindia.net/struts/strutsActionForms.shtml
But the validation doesn't seem to fire everytime. In fact it only seems to fire the first time that the method is created. Is there a better manner in which to do this so it is run everytime ?

To clarify I have a form that is created on one page, but used by several pages. Is there a way to have the validation fire more than once ? Or possibly a manner in which to specify when it runs ?

thanks in advance.
[ February 12, 2007: Message edited by: A knibbs ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The validation should fire in every action the form is used. Just make sure you specify validate="true" for every action you want to validate the form.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks for the tip. The actual problem was in an attempt to be cleaver, I was dealing with the exception earlier in the code, but not throwing any expections. Took quite some time to realize that was the issue. After that placing the validation was quite simple:

All one has to do is :
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
try
{
//code to test
}
catch(Exception e)
{
errors.add("numproducts", new ActionMessage("error.numproducts"));
}
return errors;
}
Jose Mon
Ranch Hand

Joined: Feb 13, 2007
Posts: 30
Hi,
I tried this as you said
<form name="myForm">
<field property="myInt[0]" depends="required, integer">
<arg position="0" key="myInt"/>
</field>
<field property="myInt[1]" depends="required, integer">
<arg position="0" key="myInt"/>
</field>
<field property="myInt[2]" depends="required, integer">
<arg position="0" key="myInt"/>
</field>
<field property="myInt[3]" depends="required, integer">
<arg position="0" key="myInt"/>
</field>
</form>

This is working fine if I had more than 4 textboxes. ie
myInt[0],myInt[1],myInt[2],myInt[3] are present.
But if I had only two textboxes, ie myInt[2],myInt[3] are not there, the page is not displayed. So how can I handle validations by using this technique?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
See what happens when you only define myInt[0]. Do myInt[1] and myInt[2] get validated?
Jose Mon
Ranch Hand

Joined: Feb 13, 2007
Posts: 30
If the textboxes are less in number than the index given in validation.xml,
the page is not showing any components. The page is appearing as a blank page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts validation
 
Similar Threads
actionForm mechanism
Need Help and Suggestion
Putting my head into Strurts....
Struts Validation
about struts