wood burning stoves*
The moose likes Struts and the fly likes Validating some extra fields of action form not in  my jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validating some extra fields of action form not in  my jsp?" Watch "Validating some extra fields of action form not in  my jsp?" New topic
Author

Validating some extra fields of action form not in my jsp?

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi,
I am doing validation of my form bean using struts validation framework. My actionform is getting populated by two jsps, first one is having four fields and second jsp is having same no. of fields as in actionform.

I am validating all fields of my form in validation.xml file. For my second jsp, there is no problem. Validation is fine. But for the first JSP, jsp form fields are less than action form instance fields. Since I am using required rule on some field therefore I am getting validation error for those extra actionform fields which are not in my first jsp.

One way could be by using html:hidden fields in the first form for those extra fields and pass some default values so that validation gets bypassed firts time.

Is there any other way of doing it? What about validwhen validator rule?

Tx


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Dileep Kamath
Greenhorn

Joined: Jul 15, 2006
Posts: 19
Struts looks up the attribute stored against mapping.getName(), initialises the validator with this object and then invokes the validations. So you can define the same action form class as two different form-beans and then use them accordingly in your action mapping and your validation.xml. This would let you perform different validations on the same action form class as the validations are not tied to the action form class but to the form-bean definitions.

Do post a reply if you need a sample for this.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Dileep's solution would probably work, but there's an easier way to do it.

Struts allows you to validate by either the form name or the action name. The way you're currently doing it is by the form name. If you want to validat using the action name, do the following:
  • Change your ActionForm bean so that it extends ValidatorActionForm instead of ValidatorForm
  • in your validation.xml file, instead of specifying the form name, specify the action path as the name attribute of the form stanza. example: <form name="/myAction" >
  • create a separate form stanza for each action, listing only those properties you want to be validated for that action


  • Merrill
    Consultant, Sima Solutions
    Naseem Khan
    Ranch Hand

    Joined: Apr 25, 2005
    Posts: 809
    Thanks for your reply

    The problem which I am facing is that I have just one action which is mapped to form bean and I am using the same action in two jsps, so even if I specify the action in validation.xml file instead of form name, there is no difference.

    Yes If I use two different actions in struts-config file, then sure ValidatorActionForm will work.

    Let me tell you I am doing it by passing a hidden field in both jsp like below:

    <html:hidden property="page" value="1"/>

    And in validation.xml file, field tag has a page attribute which tells you which fields are validated client side and which fields on the server side.

    Fields which I don't want to be validated in the first jsp, I pass its value more than current page value, then in that case, validations will be on client side instead of server side. Since I am not validating it on client side, it will just pass.

    Any comments?
    [ March 14, 2007: Message edited by: Naseem Khan ]
     
    jQuery in Action, 2nd edition
     
    subject: Validating some extra fields of action form not in my jsp?
     
    Similar Threads
    Actionform not populating JSP
    Focussing text fields depending on ActionErrors in struts 1.0
    Focussing text fields depending on ActionErrors in struts 1.0
    Struts Design Problem
    Some probloms with struts(New version)