Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to validate single form field using struts Validatation framework

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