File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to validate single form field using struts Validatation framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to validate single form field using struts Validatation framework" Watch "How to validate single form field using struts Validatation framework" New topic
Author

How to validate single form field using struts Validatation framework

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi,

I want to validate just one field of my form which has around 12 fields. Can I restrict validation to just one field by validation framework.

My validation.xml file is having all the field entries. (12 Field tag)

Even if I override validate method of ValidatorForm in my Form class and call super.validate(mapping, request), it validates all the fields.


Naseem
[ March 22, 2007: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Do you always need to validate ONLY one field out of the 12?
OR
Is the validation of 1 versus 12 fields conditional?

Please provide some detail regarding what you intend to achieve and why?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm assuming you're using the same form in more than one action. Otherwise, the obvious solution would be just to remove the other validations. While you can turn validation on or off for an entire form, as far as I'm aware, there is no mechanism for turning validation for individual fields on and off.

Here's something that might work for you, though. With the Struts Validation Framework, you have a choice of validating either by the form name or by the action path. You're currently validating by the form name.

The advantage of validating by the action path is that you can validate the same form differently for each action. So, if one action only needs to validate one field and another action using the same form needs to validate 12 fields, they both will work. The disadvantage to validating by action path is that if you use the same form in 5 different actions, you have to redefine the validations 5 times.

If validating by action path sounds like something you want to try, here's how you do it:
  • Modify your Actionform so that it extends ValidatorActionForm rather than Validatorform
  • Create a separate <form> stanza in your validation.xml file for each action, and specify the action path as the name instead of the form (Example: name="/myAction" )
  • List each field to be validated, just as before.

  • [ March 22, 2007: Message edited by: Merrill Higginson ]

    Merrill
    Consultant, Sima Solutions
    Naseem Khan
    Ranch Hand

    Joined: Apr 25, 2005
    Posts: 809
    I have just one action "/loan". My loan.jsp request this action. Now I have a requirement where I have to validate just one field using ajax and struts validation.

    So if user enters wrong value in a text field, onblur I am calling a javascript function where I requesting same action with form submission.


    Naseem
    [ March 22, 2007: Message edited by: Naseem Khan ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: How to validate single form field using struts Validatation framework