I don't mind the idea of combining or changing the XML
configuration for validations.  I think that the
validation rules are getting very long at the top of
the Validator and need to be moved to a separate file
(at least for the default ones).

I have some comments interspersed below on some other
issues.

David

--- Andrej Sobkowski <[EMAIL PROTECTED]> 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.
I don't see how the validator is "linked" to the
servlet.  The default method signature for Struts is
tied to Servlet specific things, but none of the
default validation use it.  The default method
signature gives you a full handle on the environment
so you can do whatever you need to do for a
validation.  Retrieve another object from scope to
compare to this one, retrieve database information,
etc.  If you want to not have this information passed
to validation methods, you can create your own
validators and override the default signature by using
the methodParams attribute.

methodParams="java.lang.Object,com.wintecinc.struts.validation.Field,java.util.List"

Look at the section on validating outside of Struts.
http://home.earthlink.net/~dwinterfeldt/overview.html#validatingOutsideOfStruts


> 
> 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?
Well the ActionForm isn't really aware of how it is
validated when you use the validation framework.  It
just has a validate method that calls something else
that is aware how it should be validated.

> 
> 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>
I think it is good to have the validator defined
elsewhere so there can be defaults and if you want
matching Javascript for this it would make the action
very hard to read by placing the validator reference
here.  That doesn't mean that arguments could be
passed in here though.

> 
> * 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
> 


__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

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

Reply via email to