e.g.
/foo.do?method=save
mapping.getParameter() return the name of the parameter used to dispatch with. In this case "method"
request.getParameter(mapping.getParameter()) returns the value in the parameter map for the dispatch in this case "save"
you can then check if in this is a dispatchaction you wish to validate and go from there.
You can call super.validate() to use validations in the validation.xml file, although I have a personal preference to just use action form and validate using java (old struts 1.0) rather than use validator framework as I think it has greater clarity than using the validator.
I originally learnt this here posted by Haroon Rafique http://raibledesigns.com/wiki/Wiki.jsp?page=ValidatorDispatchAction
ActionErrors errors = new ActionErrors(); String parameter = mapping.getParameter(); if(parameter == null) { return null; } /* we only need to validate when the save method is called */ if(parameter != null) { String name = request.getParameter(parameter); String message = Resources.getMessage(request,"button.save"); if(name == null || !name.equals(message)) { return null; } }
//rest of the validations here
The reference provides a means of using the validator framework, which I just cant be bothered with anymore as IMO overriding the validate method is a lot clearer in terms of design what the action form is for, and when you need to validate uploads or other more complex validations everything is in the same place. The disadvantages of what I do is that you don't have the javascript generated for you, and validation is harder to palm off on a site builder (this would depend on who in your team deals with this stuff). There are other advantages to using action forms over dynaaction forms that go beyond the scope of this response.
Mark
On 16 Jun 2004, at 03:40, Niall Pemberton wrote:
DynaValidatorForm has been refactored slightly to make it easier to override
the key to use for the validator plugin (available in the nightlies and will
be in next release of Struts). If you extend DynaValidatorForm and override
the getValidationKey() method maybe you could use combination of the form
and method name for the validator plugin rules.
For an example of this look at DynaValidatorActionForm:
http://cvs.apache.org/viewcvs.cgi/jakarta-struts/src/share/org/apache/ struts/validator/
Niall
----- Original Message ----- From: "Josh Holtzman" <[EMAIL PROTECTED]> To: "'Struts Users Mailing List'" <[EMAIL PROTECTED]> Sent: Tuesday, June 15, 2004 10:18 PM Subject: RE: LookupDispatchAction and Form validation + DispatchAction
validationDavid,
I too am having a similar concern, albeit, with the DispatchAction class.
I am currently have a form that is backed by a DynaActionForm andwithinoccurs through the Validator pluggin.
When binding the validation routine to the action by specifying
"validate='true'" in my Action configuration (within the struts-config.xml
file) I believe it will run my validation against all methods calledthe DispatchAction class for this action. In my situation, I only wantthevalidation to be performed on 2 of the 6 methods within my DispatchActionvalidation,
class.
Is there a way to do this while still using the Validator Pluggin and a
DynaActionForm?
Here are some alternatives I have devised: 1. Create 1 DispatchAction class to handle the two methods withsinceand 1 for all other methods.
2. Do not use the Validator pluggin, and perform validation within the
DispatchAction class only when the specific methods are called.
I don't like either of these options very much (particularly number 2)usethey require a significant increase in maintenance.
Any suggestions would be greatly appreciated.
Thanks.
Josh Holtzman
AMERICAN DATA COMPANY
Developing and Supporting your Online Applications
[EMAIL PROTECTED]
Voice: (310) 470-1257
Fax: (310) 362-8454
Sun Microsystems iForce Partner
-----Original Message----- From: Ellingson, David [mailto:[EMAIL PROTECTED] Sent: Tuesday, June 15, 2004 1:50 PM To: '[EMAIL PROTECTED]' Subject: LookupDispatchAction and Form validation
I am trying to implement a LookupDispatchAction class, and I'm not sure if
I'm trying to use Form validation in an unintended manner.
I see that the LookupDispatchAction class is designed to handle multiple
methods, but I do not see a corresponding way to handle multiple
validations. As the value of the "parameter" attribute is available from
within the ActionForm.validate() method , it seems like I could somehowthis to determine how to validate the form. However, that would requiremeto hardcode against the value from the html page/Resource file.
1) Is there a way to perform validation based on the method being performed? 2) If not, is there a reason for this restriction? 3) Am I supposed to create separate Action and Form classes in this situation?
Thanks, Dave
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]