• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Struts: How to validate list

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Tankred Smult
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic