aspose file tools*
The moose likes Struts and the fly likes Struts 2 Validator Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 Validator Question" Watch "Struts 2 Validator Question" New topic
Author

Struts 2 Validator Question

Kelvin Tan
Greenhorn

Joined: May 02, 2009
Posts: 10

Hi Guys,

I have this question about the validator in the Struts2 framework.

Say I have the following,

Action Class:
Book.java


Video.java


Book-validation.xml (with the following codes)


add.jsp



struts.xml



Now the question is, why does the validator validates the field "title" that in the Add Video section? Doesn't the validator only validate fields that
will be invoked by the addBook.action only?

I'm confused by how the validator works, (sorry I only picked up Struts 2 recently and has no prior experience with any J2EE framework) hopefully I can get some help from the Ranch.

Thank you guys.
Best Regards.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you saying that if you click on the "Submit Media" button it's validating the title? I'm a little skeptical, since the Video class doesn't extend ActionSupport and shouldn't support validation at *all*.
Kelvin Tan
Greenhorn

Joined: May 02, 2009
Posts: 10

Thank you for your reply David.

The problem is whenever the "Submit Publication" button is clicked, the validator also validates the "title" field for the Video section. Which is something I don't understand.

Since the validation xml is named under "Book-validation.xml", isn't it suppose to validate only fields that belong to the Book action class? Or am I having the wrong concept, that the validation just looks at the field name in the jsp and validate it as long as it matches?

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not validating the video title--it's displaying the validation message for book title, because you have no way of differentiating between the two title fields. S2 validation is field-based, not form-based. You have the same field name in two forms. The action's fieldErrors() map is at the page level (not form). So the <s:textfield> tag checks for "title" errors--which it finds. Twice.
Kelvin Tan
Greenhorn

Joined: May 02, 2009
Posts: 10

Oh..ok so I understand.

Thank you for your help.

Another question, is there any solution to get round this problem?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Name the fields differently? Use a bean-per-form? Don't use two forms on the same page? Use two submit buttons and a single form?
Kelvin Tan
Greenhorn

Joined: May 02, 2009
Posts: 10

Thank you very much David.

Appreciate it =)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 Validator Question