I got the first point mentioned where I would need to override the
validationKey method and not the second one. 

Problem here is it is not only required during login time but throughout
the application there can be different validations for each form or some
of the forms. Hence if I follow the first point mentioned I would need
to have different forms entry throughout the application.



-----Original Message-----
From: Scott Van Wart [mailto:[EMAIL PROTECTED] 
Sent: Thursday, June 15, 2006 7:08 PM
To: Struts Users Mailing List
Subject: Re: Help on Validation

Vishal Seth wrote:
> Hi,
>
>     Can I do validation for same form on basis of some session key.
> Currently all the validations are stored in application scope.
>
>     I want depending upon user logged the validation fired should be
> different on same form. I would be having different validation files
for
> different users. It would be something like resource files are picked
as
> per locale I want similarly validation files are loaded as per user.
>   
You might want to look at getValidationKey() in 
org.apache.struts.validator.ValidatorForm (presumably this is the base 
class of your action form, otherwise validations wouldn't work).

If you override getValidationKey(), you can return values that specify 
which validation should be used.

The default behavior is to return the form name that you have in your 
<form-bean name="(here)"... /> in your struts-config.xml.  And then you 
generally put the same value in your validations.xml <form 
name="(here)"... />.  BUT, if you override getValidationKey(), you can 
do stuff like this (in your ValidatorForm-derived class):

public String getValidationKey( ActionMapping mapping, 
HttpServletRequest request )
{
    return "loginFor" + this.userName + "Form";
}


And then in your validations.xml:

<form name="loginForJoeForm">...</form>

<form name="loginForMaryForm">...</form>

A problem with the example I gave above: The user can obviously type in 
whatever user name they want.  You usually do checking for this in your 
Action, not the form (since the form is for simple validations).  In 
fact, getValidationKey() gets called near the start of validate().  So 
you might have to refactor things out a little bit to keep the heavy 
validations in your Action-derived class.  One way is to chain actions 
together... not sure why I mentioned it, since there's a far better way:

YOU tell Struts when to do validation:

<action type="mypkg.LoginAction" name="loginForm" 
validate="false">...</action>

The important part there is the validate="false".  Now we can call our 
validation routine on our own (which will use the usual validation 
mechanism):

public class LoginAction extends Action {

  public ActionForward execute( ActionMapping mapping, ActionForm form, 
HttpServletRequest request, HttpServletResponse response ) {

    LoginForm loginForm = (LoginForm)form;
    // do simple validity check on loginForm.getUserName(), make sure 
it's there
    // that it's valid in the database, before we let the form do its 
validations
    // ...

    ActionMessages errors = loginForm.validate( mapping, request );
    // getValidationKey gets called somewhere in the validate() method

    if ( errors == null ) {
      errors = new ActionMessages();
    } else if ( !errors.isEmpty() ) {
      saveErrors( request, errors );
      return mapping.findForward( "failure" ); // (or 
"validationFailed", or "accessDenied", or "returnToLogin", whatever you 
want)
    }

    // Validations passed, now let's do password checking, etc...
    // ...

  }

}

Good luck,
  Scott


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





MASTEK 
"Making a valuable difference"
Mastek in NASSCOM's 'India Top 20' Software Service Exporters List.
In the US, we're called MAJESCOMASTEK

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Opinions expressed in this e-mail are those of the individual and not that of 
Mastek Limited, unless specifically indicated to that effect. Mastek Limited 
does not accept any responsibility or liability for it. This e-mail and 
attachments (if any) transmitted with it are confidential and/or privileged and 
solely for the use of the intended person or entity to which it is addressed. 
Any review, re-transmission, dissemination or other use of or taking of any 
action in reliance upon this information by persons or entities other than the 
intended recipient is prohibited. This e-mail and its attachments have been 
scanned for the presence of computer viruses. It is the responsibility of the 
recipient to run the virus check on e-mails and attachments before opening 
them. If you have received this e-mail in error, kindly delete this e-mail from 
all computers.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to