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]