Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dileep Kamath
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  •  
    Naseem Khan
    Ranch Hand
    Posts: 809
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic