You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2002/12/18 21:24:32 UTC

DO NOT REPLY [Bug 15499] New: - Validator does not successfully validate empty date fields. (#10349 revisited)

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15499>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15499

Validator does not successfully validate empty date fields. (#10349 revisited) 

           Summary: Validator does not successfully validate empty date
                    fields. (#10349 revisited)
           Product: Struts
           Version: 1.1 Beta 2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Validator Framework
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: bruce.barton@doit.wisc.edu


In bug #10349 it was reported that Validator does not successfully validate 
empty date fields. It appears to me that the description of the problem and 
solution in that report is incorrect.  

StrutsValidator.validateDate returns null when the field value is blank or 
null. Commons Validator passes the result of the call to validateDate to its 
isValid method:

   private boolean isValid(Object result) {
      boolean bValid = false;

      if (result instanceof Boolean) {
         Boolean valid = (Boolean)result;
         bValid = valid.booleanValue();
      } else {
         bValid = (result != null);
      }

      return bValid;
   }

And so we see the "invalid date" result from a call with an empty field. 

I propose having StrutsValidator.validateDate return a dummy Date object when 
the field is empty.  

A revised validateDate method is:

    public static Date validateDate(Object bean,
                        ValidatorAction va, Field field,
                        ActionErrors errors,
                                    HttpServletRequest request) {
        Date result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
           value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }
        String datePattern = field.getVarValue("datePattern");
        String datePatternStrict = field.getVarValue("datePatternStrict");
        Locale locale = StrutsValidatorUtil.getLocale(request);

        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (datePattern != null && datePattern.length() > 0) {
                    result = GenericTypeValidator.formatDate(value, 
datePattern, false);
                } else if (datePatternStrict != null && datePatternStrict.length
() > 0) {
                    result = GenericTypeValidator.formatDate(value, 
datePatternStrict, true);
                } else {
                    result = GenericTypeValidator.formatDate(value, locale);
                }
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            if (result == null) {
                errors.add(field.getKey(), StrutsValidatorUtil.getActionError
(request, va, field));
            }
        } else {
            result = new Date();
        }
        return result;
    }


This approach appears to work in the testing I've done here.  It allows me to 
validate non-required date fields.

It appears to me that the same problem arises for all of the numeric type 
checks in StrutsValidator as well.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>