Meaningless Drivel is fun!*
The moose likes Struts and the fly likes validator, same field, same formbean, diff JSP,diff validations 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 "validator, same field, same formbean, diff JSP,diff validations" Watch "validator, same field, same formbean, diff JSP,diff validations" New topic
Author

validator, same field, same formbean, diff JSP,diff validations

Arthur Ford
Greenhorn

Joined: Sep 06, 2006
Posts: 22
Hi,
I have a form bean


I use this ActionForm bean in 2 jsps.

JSP1,JSP2

In JSP1, I need to validate the empNo field to be 'required'.
In JSP2, I need to validate the empNo field to be 'required' and 'integer'.

Ofcourse, all the other 20 properties have their own validations..

Right now, what I am doing is I have given a different name to the actionform bean in struts-config and have repeated the validations in validation.xml.

But what is ugly that just for one field, 20 of them are getting repeated..

Is there any way out??

and then
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
what you could do is:
- write one declarative validation which checks required
- write your own 2 lines of business-logic in the appropriate action which valides for integer if necessary

this would allow you to live with just one configuration...

would that help?

jan
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
do both the JSP points to the same action? I believe you can get around without coping the whole form twice. In validation.xml file the field element has an attribute "page".

Steps to try:
- Add an int field called page in the form.
- When JSP1 is being rendered render this field as hidden with value 0
- When JSP2 is being rendered render this field as hidden type with value 1
- In the validation.xml file create two field elements for form member that requires different validation on JSP1 and JSP2 with page value as 0 and 1
- For the rest of form members set the page value to 0

Validator doc says

All validation for any field on a page less than or equal to the current page is performed server side. All validation for any field on a page equal to the current page is generated for the client side Javascript. A mutli-part form expects the page attribute to be set.


- Only question I have is will Validator framework evaluate all the dependency if the dependencies are defined in more than one field element for the same element. I guess it's worth trying.

- As long as you don't enable client side validation this should be fine.

I couldn't try myself right now. Let us know if it works.
[ September 26, 2006: Message edited by: Purushothaman Thambu ]
Arthur Ford
Greenhorn

Joined: Sep 06, 2006
Posts: 22
Thnks guys for the reply. I tried the page method. It didnt work. So, I just changed my JSP page, and wrote Javascript for the page. Didnt use the validation framework for this page.

I had a deadline, so coudlnt experiment a lot. Maybe will try later.
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hello,

In a similar example, I am getting

13:01:07,625 ERROR [[action]] Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at strutstutorial.UserRegistrationForm.validate(Unknown Source)

Any clues on where I am going wrong?

Rgds,

Seetesh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: validator, same field, same formbean, diff JSP,diff validations