aspose file tools*
The moose likes Struts and the fly likes Struts: How to validate list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: How to validate list" Watch "Struts: How to validate list" New topic
Author

Struts: How to validate list

Tankred Smult
Greenhorn

Joined: Jul 15, 2003
Posts: 16
My question is how can I dynamically assign arg0 in the validator?

Here's the senario:

I have a list of questions which I get from a database, and I would like to put back answers. In my form-bean I have a property called "lines", which contain lines with questions and answers. I use like this (pseudocode):



All the questions has to be answered, and I'm trying to do that with the validator framework (struts 1.1), like this:


the properties file:


So, if a user doesn't answer a question, she will get an error saying "You must answer the question 'Question'" (that's what's in my properties file). However, I would like to use the "question" property of the bean which contains the actual question as arg0. So it would say
"You must answer the question 'Did you like the movie?'".

So maybe something like this instead (except it should work) :-)


Can this be done? Does anyone know how?
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Using the validation framework is tricky when you've a dynamic number of fields. Here is my understanding of how to handle it:

The fields specified in the validation framework must match what is getting submitted from the enduser's page. In your case of indexed properties it would come in as answer[0], answer[1], answer[n]...

So every possible field would be required in the framework. This means you have to guess at the maximum number of questions and list that many in your xml.

Further, you need to know how many are required on each page. Otherwise you may have guessed too high and it will demand that questions are required that may not even exist on a page. (Sounding like a cellphone commercial) This means you'd need to add something like hidden indexed values that you can use in a requiredif validation. In other words, answer[0] is requiredif required[0] exists.

The easiest thing to do might be to just write your own validation method...


A good workman is known by his tools.
Tankred Smult
Greenhorn

Joined: Jul 15, 2003
Posts: 16
Actually you don't have to guess on the actual number. Using the validator like I did in the validators.xml
[CODE]
<field property="answer" indexedListProperty="linjes" depends="required">
[CODE]
does actually validate the questions the way it should. The indexedListProperty is the property of the form-bean that will return a list, and "answer" is a property of the bean that exist in that list.

And this works just fine, if the user doesn't answer all the questions, I will get an validation error. And is a new question is added to the datebase, it too will be required with no change to the xml-document.

My problem is the errormessage that are shown to the user if he doesn't fill out the question. Then he will get an errormessage saying "You will have to answer the question".

But my bean that exist in the list (the one with the "answer" property also has a "question" property, that will return the actual question asked. So I would like struts to use the "question" property of that bean as arg0 (to the error message) instead of a statically and general parameter from my properties file.

So, it's not the validation itself that's the problem, it's the error-message to the user.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Hmm. You're right. I guess it's worked for three years that way. I'm having a bad day!
Anyway, if you wrote it yourself you could easily accomplish it.
errors.add("myError", new ActionError("error.iWantTheTruth", lines[i].getQuestion());
or something like that.
The only thing I could think of using the framework is to set the questions as hidden properties, put a var-name for it, and then try a regexp ${} in the arg0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts: How to validate list
 
Similar Threads
Displaying matrix type of questions in struts?
validating collections
Help with Indexing problem using logic:iterate and nested:iterate
Validation of indexed properties
Indexed properties and html:javascript