This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes Struts 2 validate dynamic generated list (collection validator) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 validate dynamic generated list (collection validator)" Watch "Struts 2 validate dynamic generated list (collection validator)" New topic
Author

Struts 2 validate dynamic generated list (collection validator)

Damian Contreras
Greenhorn

Joined: Oct 11, 2009
Posts: 3
I'v done several research to find an answer to this topic. I'm so frustrated with Struts 2 'cause is unconceivable that this common problem, when you have to make a CRUD of an invoice and invoice detail with dynamic rows, isn't resolved yet.

Suppose I have these beans:



On the other hand I have an Action:



On the interface I use some ajax to create dynamically rows with the detail of the invoice. To fill the ValueStack with:

invoice.detail[0].product
invoice.detail[0].quantity
invoice.detail[0].price
invoice.detail[1].product
invoice.detail[1].quantity
invoice.detail[1].price
....

All I want is to validate if a conversion error ocurred or if the user left some fields empty. But notice that the list indexes are generated dinamycally so it seems impossible to use some *-validation.xml to do the validation.

A incomplete way to get around is to iterate invoice.getDetail() in validate() method at the Action. But it isn't what I need because I need to know when the user put invalid data or if the user left empty some field in the detail, and make short-circuit the conversion error and the other validations.

In my research I found that some years ago WebWork had a collection validator:

http://www.opensymphony.com/webwork/wikidocs/collection%20validator.html
http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/validator/validators/CollectionFieldValidator.html

And it totally would solve the problem. But it seems that this improvement isn't included in Struts 2.

Any suggestions to my problem?
Damian Contreras
Greenhorn

Joined: Oct 11, 2009
Posts: 3
Anyone?
Neil Beveridge
Greenhorn

Joined: Nov 18, 2009
Posts: 1
Sorry if you have progressed since your last post. I would solve this problem by using annotations to define the validation and place the annotations on the model beans themselves. This way the validation is applied uniformly and independent of the number of instances actually submitted.

You should put a class level annotation as follows:


On the getter for the InvoiceDetail collection you need the following. This annotation tells Struts 2 to check the objects on the Collection for validation instructions. To read more see Struts2 VisitorFieldValidator.


On your model bean you then need the @Validation annotation again and then your actual property annotations on the setters. For example I use the following in one of my beans:



This just checks that name is set and it is not an empty string. I should mention that the message properties of the above annotations are arbitrary and must be set but it is up to you whether you make use of the output - I usually prefer to just use a CSS error class to highlight the field.

Hope this helps.
Neil
Damian Contreras
Greenhorn

Joined: Oct 11, 2009
Posts: 3
Thank you !!! It was just what I wanted.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts 2 validate dynamic generated list (collection validator)