I actually built out a very similar framework back in my ATG days. 
 Greate minds stink alike....

Not so sure how much logic you want to put into the XML.  I would rather 
have Form validators that have access to a slew of page validation rules.

One possiblity is

MyFormValidator extends FormValidator(){
 
 public void validate(ActionForm form){
    MyForm myForm = (MyForm)form;
 
     validateNotBlank(myForm.getName(), BLANK_MESSAGE);
     validateWithinRange(MIN_VAL, MAX_VAL, myForm.getIntVal(), 
OUT_OF_RANGE_MESSAGE);
   }
}

Obviously this is very similar to what you would have written in the XML 
but with three advantages:
1.  You can do much more complex logic in the Java code
2.  You can run a test like this in an offline (not running 
ServletEngine) environment to check your business logic.
3.  You don't end up with all your coders vying for control of the 
struts-config.xml file with the corresponding merge problems.

However, I don't think anything you have specified precludes doing work 
in the Java level, just that it is possible in the struts-config.xml level.

Validators would all be application scope beans and designed to be 
Stateless.  You can have one validator call another, and so on.  

I guess that this does not preclude you from doing simple Validation in 
the XML.  And yes, you could do it in a onther file as you suggested 
below.  But I prefer to thin of the XML files as a registry:  They are 
the way one thing finds another.  Keep your serious logic in the Java code.

And please do not name interfaces with Hungarian Notation (IValidator) 
 Aside from violating the Java coding standards, it give me flashbacks 
to my Microsoft Visuall C++ days.  Shudder.
  May I suggest in its place the more friendly Validatable?  Actually, 
you probably will find that this should be an abstract class that you 
have to fill in the blanks on.  The one I wrote before returned an 
Exception that was just a collection of the FormValidation errors, with 
messages attached for display purposes.  Then you provide method to dump 
the results of an internal Validation into the scope of the current 
validation.


Another suggestion.  For the inistialization of the Validators,  use XML 
like

  <validatator
               name="lastNameValidator"
               property="lastName"
               validator="com.mycompany.SizeValidator"
               message="Last Name is mandatory and can't be longer than 
15 chars" >
      <param name="minimum" type="java.lang.Integer"  value="3" />
      <param name="maximum" type="java.lang.Integer"  value="15" />
</validation>

Where each of the parameters are set according to the rules of java bean 
properties.


and then in the action
<validation name="lastNameValidator" />


Actually, this would be nice to have for form initialization as 
well....hmmmmm.


Andrej Sobkowski wrote:

> Hello All,
>
> I have a set of design considerations/suggestions for the Validation 
> Framework in Struts. Let me know if I'm totally missing the point.
>
> I like the whole Validator approach, in particular the XML 
> configuration. I believe that's the way to go, but I'd like to discuss 
> a few points.
>
> DISCUSSION: The Validator is quite "linked" to Servlet's stuff 
> (HttpServletRequest) and to FormBean. If I got it right, currently the 
> Validator needs a set of parameters to be passed to the 
> ValidatorAction, including the bean to validate, the Field and some 
> Servlet parameters.
>
> In a similar way, the validation process is linked to the FormBean 
> (validate() in FormBean class).
>
> Shouldn't they be separate?
> - The Action should take care of the HTTP stuff while the Validator 
> should only have knowledge of the bean and the corresponding fields to 
> be validated.
> - The form bean itself is a "special data holder" and shouldn't be 
> aware of how its data is validated. Do you agree?
>
> I was thinking at something like the following (pseudo-code):
>
> * CONFIGURATION file (new DTD for struts-config.xml or separate file)
>  <action    path="/login"
>             type="com.mycompany.myActionWithValidation"
>             name="myForm">
>    <!-- add validation on myForm's property 'lastName' that will check 
> via a SizeValidator
>         that the size of the field is between 1 and 15 chars. If not, 
> the message will be
>         returned in the ValidationException (I18N can be added easily) 
> -->
>    <validation property="lastName"
>                validator="com.mycompany.SizeValidator"
>                arg0="1"
>                arg1="15"
>                message="Last Name is mandatory and can't be longer 
> than 15 chars" />
>    ...
>  </action>
>
> * JAVA CODE
> public interface IValidator(Object bean) {
>  +validate() throws ValidationException;
> }
>
> public class Action {
>  ...
>  +addValidator(IValidator val)
>  +validators(): Iterator // returns an Iterator on all validators for 
> the action
> }
>
> // Validator that checks if text size is >min and < max (for example).
> // It can be easily extended to check int min/max and so on.
> public class SizeValidator {
>  ... // min/max
>
>  public void validate(Object bean) throws ValidationException {
>    Object value = getPropertyValue(bean);
>    if (value instanceof String) {
>      // Check String size
>      String check = (String)value;
>      if ((check.length() > maxSize) ||
>          (check.length() < minSize)) {
>          // Validation failed: throw exception
>          // with corresponding error message (defined in conf)
>          throwException(getValidationMessage());
>      }
>    } else {
>      // Error, wrong class type...
>    }
>  }
> }
>
>
> // Minor changes to ActionServlet
> public class ActionServlet {
>  ...
>  public void processInitValidatorsForAction(..) {
>  // By reading the XML configuration file, the ActionServlet will 
> execute a set of addValidator(..)
>  // For example, consider the following XML struts-config.xml (DTD to 
> be modified)
>  // <action
>  curAction.addValidator(new SizeValidator("lastName", "1", "15");
>  curAction.addValidator(new RegExpValidator("phone", "(999)999-9999");
>  ...
>  }
>
>  // Executes all validators on FormBean(s) for action executed
>  public ActionErrors processValidate(...) {
>    Iterator it = action.validators();
>    try {
>      ((Validator)it.next()).validate(formBean); // the validator 
> validates the form bean only
>    } catch (ValidationException e) {
>      Log.debug("Validation failed!", e);
>      errors.addError(e.getMessage()); // Add multilanguage
>    }
>  }
> }
>
> Useless to say, this is only a high-level point of view. It does work 
> on my prototype (no XML config), but it can be enhanced and optimized 
> in many ways. I think it's also conceptually pretty close to what is 
> currently done in the Validator.
>
> The Validators can be designed as desired. To compare two values, 
> simply define a validator like CompareValidator(property1, property2, 
> compareRule) with compareRule = "<", ">",...
>
> What do you think?
>
> Thanks.
>
> Andrej
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at 
> http://explorer.msn.com/intl.asp
>
>
> -- 
> To unsubscribe, e-mail:   
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: 
> <mailto:[EMAIL PROTECTED]>
>
>
>



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

Reply via email to