Uploaded image for project: 'Struts 1'
  1. Struts 1
  2. STR-2854

mask validator error

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.2.7
    • 1.3.3
    • Core
    • None
    • Windows XP, Weblogic 8.1 SP5
    • Patch, Important

    Description

      I have a problem with the mask validator when it is used without others validators such as "required" in struts 1.2.7.
      For example, I have a field which depends of a radio button selecction (requiredif in previous versions). In this case I'm using the "validwhen" and the "mask" validators because that field is an string with a date format (dd/mm/yyyy), so, i need to check it (optional fields with a special format. For that reason, I can't use the "required" rule). The problem is when it is filled in with only blank spaces. In this case, the "mask" validator doesn't work.

      <field property="txtDate" depends="mask,validwhen">
      <msg name="mask" key="errors.invalid"/>
      <arg position="0" key="Año" resource="false"/>
      <var>
      <var-name>mask</var-name>
      <var-value>${formatDate}</var-value>
      </var>
      <var>
      <var-name>test</var-name>
      <var-value>(((rdSelector==0) and (this !=null )) or (rdSelector==1))</var-value>
      </var>
      </field>

      where:
      ${formatDate} = ^[0-9][0-9][/][0-9][0-9][/][0-9][0-9][0-9][0-9]$
      rdSelector is my radio button field
      txtDate is a date textfield.

      I think, the problem is in the validateMask method of the FieldChecks class (org.apache.struts.validator):

      public static boolean validateMask(Object bean,ValidatorAction va, Field field,ActionMessages errors, Validator validator, HttpServletRequest request) {

      String mask = field.getVarValue("mask");
      String value = null;
      if (isString(bean))

      { value = (String) bean; }

      else

      { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); }

      try {
      if (!GenericValidator.isBlankOrNull(value) // <b>THIS IS THE PROBLEM</b>
      && !GenericValidator.matchRegexp(value, mask))

      { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; }

      else

      { return true; }

      } catch (Exception e)

      { log.error(e.getMessage(), e); }

      return true;
      }

      The problem I found is in the second if:
      if (!GenericValidator.isBlankOrNull(value) && ......

      this means that if the field is empty, null or with only blank spaces as the isBlankOrNull method does, the field is not considered as invalid (the if block doesn't execute, so no errors are generated), and the second condition never executes because of the && operator, so, the Regular expression is never checked..

      I think , the if statement should be something like this ...

      if (value != null && value.length()>0
      && !GenericValidator.matchRegexp(value, mask)) {

      it is almost the same as the original if, but the difference is that it also includes strings with only
      blank spaces, and in this case the validator returns false just as it should be ..

      have you seen any problem like this?, can be fixed this problem in new releases? or there are some other solutions to solve it ?

      Thanks for your help.

      Attachments

        Activity

          People

            mrdon Donald J. Brown
            jorgerdc Jorge Rodríguez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: