I tend to use the DispatchAction mechanism for forms that are used to do
create/update/delete of objects. The pattern I have fallen into is having
"cancel" be another one of the dispatch methods in this case. I have
validation turned on for the form, but, conditionally disable the validation
both on the client-side and on the server-side based upon the setting of the
dispatch method.

It is actually quite easy to do for the client-side implementation. You
already have to explicitly provide the onSubmit event handler for the form
anyway. No one is forcing you to call the JavaScript function generated by
the <html:javascript/> tag. So, I have my own JavaScript function that
conditionally calls the validateYourFormNameHere() function based on the
setting of the dispatch method. An example of mine looks like this:

  function handleFormValidation(form) {
    result = true;

    if (form.dispatchMethod.value == "save") {
      result = validateModifyUserForm(form);
    }

    return result;
  }

On the server-side, this does require a form class where you can put the
conditional logic in your validate() form function override. However, this
doesn't prevent me from using DynaForms for the tedious getters and setters.
It just means I have to extend the flavor of DynaForm I am using depending
on whether action-based or form-based validation is being used. In the form
class, you only override the validate function and conditionally call the
super.validate() based upon the setting of the dispatch method in the form.
You can still define all the form properties in your Struts configuration
file.

I'm not thrilled with this approach, but, it does work within the current
architecture to make the validation somewhat dynamic without being overly
complex. Your mileage may vary. :-)

- Mike Van Riper
  mailto:mvanriper@;verisign.com

P.S. Having the "cancel" method in the same DispatchAction subclass that
handles creates/updates/deletes has worked nicely for workflows that are
multiple steps too. I tend to use session scope beans in that case both for
the form and for related workflow data. So, I can have common logic for
releasing session scope resources for the workflow in one common action for
create/update/delete/cancel.

> -----Original Message-----
> From: Thomas Delnoij [mailto:t.delnoij@;imn.nl]
> Sent: Wednesday, October 30, 2002 6:58 AM
> To: Struts Users Mailing List
> Subject: Struts Validator / dynamically enabling/disabling
> 
> 
> Hi.
> 
> I am using the Struts Validator in conjunction with a 
> ValidatorForm. My html
> form has two submit buttons, one for saving without 
> validation, another one
> with validation.
> 
> Is it possible to dynamically switch on and of the validator 
> depending on
> which button is pressed?
> 
> I could of course dynamically set the action attribute of the 
> form tag,
> using JavaScript. But I don't want to depend on JavaScript 
> being switched
> on/off in the clients browser.
> 
> Another possibility, that didn't convince me up intil now, is 
> writing a
> custom validate method, that checks for the value of the 
> clicked button.
> This method could be called before every other validate 
> method and return
> false in the case no validation is necessary and an empty ActionErrors
> Object to make the framework assume everything is OK. Would this work?
> 
> Any suggestions?
> 
> Thomas Delnoij
> Internet Engineer
> IMN - SSE Baarn
> 
> Mobile: +31 6 144 300 14
> EMail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@;jakarta.apache.org>

Reply via email to