aspose file tools*
The moose likes Struts and the fly likes Struts validation problem with string array for text fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts validation problem with string array for text fields" Watch "Struts validation problem with string array for text fields" New topic
Author

Struts validation problem with string array for text fields

P Currid
Greenhorn

Joined: Apr 07, 2009
Posts: 6
Hi,
I have a requirement for struts validation for a number of text fields which will use the same name. As the number can be up to 50 text boxes, obviously, I want to use the same name.
The name of my text box if firstName[]

Below is my form beans:
package strutspackage;

import org.apache.struts.validator.ValidatorForm;

public class appForm extends ValidatorForm {

private static final long serialVersionUID = 1L;

private String[] firstName;

public String[] getFirstName() {
return this.firstName;
}
public void setFirstName(String[] firstName) {
this.firstName = firstName;
}
}

My validation is a simple 'required' validation:
<field
property="firstName"
depends="mask"
page="1">
<arg key="genericInvalid.amount"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>

However, when I run this application form, I'm getting errors:
javax.servlet.ServletException: Invalid argument looking up property firstName[] of bean org.apache.struts.taglib.html.BEAN
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.appForm1_jsp._jspService(appForm1_jsp.java:172)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I presume this has something to do with my form class.
I don't have much experience using string array within Struts so if anyone can lend a hand or has previous experience with this, it would be much appreciated.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please see the following FAQ entries:

- JavaRanch name policy
- UseCodeTags

IIRC you can't validate over an array using XML validation; you may need to validate array fields in the form's validate method.
P Currid
Greenhorn

Joined: Apr 07, 2009
Posts: 6
apologies. I'll update this.
I'm not quite sure how to go about this type of validation....could you give me some pointers?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Loop over the array and make sure there are values in it?
P Currid
Greenhorn

Joined: Apr 07, 2009
Posts: 6
I was thinking about doing the validation in the actionform, but in reality, I'm not sure how to do this. I use struts quite irregularly, so wouldn't be experienced enough.
If I could set the firstname[] string, and loop through this within the actionform?
Not sure how to go about this??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts validation problem with string array for text fields
 
Similar Threads
Validation error
Mask validation error in struts 1.2
how to do validation in struts1
How to display error message
how to validate multiple text boxes of same name