Issue Details (XML | Word | Printable)

Key: STR-2854
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Critical Critical
Assignee: Don Brown
Reporter: Jorge Rodríguez
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Struts 1

mask validator error

Created: 03/May/06 05:31 AM   Updated: 09/May/06 10:13 PM
Component/s: Core
Affects Version/s: 1.2.7
Fix Version/s: 1.3.3

Time Tracking:
Not Specified

Environment: Windows XP, Weblogic 8.1 SP5

Resolution Date: 07/May/06 12:34 PM
Flags: Important, Patch
Labels:


 Description  « Hide
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.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Jorge Rodríguez made changes - 03/May/06 05:35 AM
Field Original Value New Value
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 integer data type, so, i need to check it. The problem is when it is filled in with only blank spaces. In this case, the "mask" validator doesn't work.

<field property="txtAnio" 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>${numeros}</var-value>
             </var>
        <var>
           <var-name>test</var-name>
           <var-value>(((rdSelector==0) and (*this* >0 )) or (rdSelector==1))</var-value>
        </var>
  </field>
where:
${numeros} = ^[0-9]*$
rdSelector is my radio button field
txtAnio is a year 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 part of that if never executes because of the && operator so, the Regular expression is never checked.. so it is almost right, but fails with blank spaces ..

do you have any options to solve it ?,
Ineed to change the struts.jar ?, I checked the same class in struts 1.2.9 and the problem remains.

Thanks for your help ..



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 integer data type, so, i need to check it. The problem is when it is filled in with only blank spaces. In this case, the "mask" validator doesn't work.

<field property="txtAnio" 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>${numeros}</var-value>
             </var>
        <var>
           <var-name>test</var-name>
           <var-value>(((rdSelector==0) and (*this* >0 )) or (rdSelector==1))</var-value>
        </var>
  </field>

where:
${numeros} = ^[0-9]*$
rdSelector is my radio button field
txtAnio is a year 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 part of that if never executes because of the && operator so, the Regular expression is never checked.. It is almost right, but fails with blank spaces ..

do you have any options to solve it ?,
Ineed to change the struts.jar ?, I checked the same class in struts 1.2.9 and the problem remains.

Thanks for your help ..



Jorge Rodríguez made changes - 04/May/06 05:49 AM
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 integer data type, so, i need to check it. The problem is when it is filled in with only blank spaces. In this case, the "mask" validator doesn't work.

<field property="txtAnio" 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>${numeros}</var-value>
             </var>
        <var>
           <var-name>test</var-name>
           <var-value>(((rdSelector==0) and (*this* >0 )) or (rdSelector==1))</var-value>
        </var>
  </field>

where:
${numeros} = ^[0-9]*$
rdSelector is my radio button field
txtAnio is a year 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 part of that if never executes because of the && operator so, the Regular expression is never checked.. It is almost right, but fails with blank spaces ..

do you have any options to solve it ?,
Ineed to change the struts.jar ?, I checked the same class in struts 1.2.9 and the problem remains.

Thanks for your help ..



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* >0 )) 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.

Jorge Rodríguez made changes - 04/May/06 05:55 AM
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* >0 )) 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.

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.

Repository Revision Date User Message
ASF #400426 Sun May 07 05:33:43 UTC 2006 mrdon Allowing the mask validator to work on strings with just spaces
STR-2854
Files Changed
MODIFY /struts/action/trunk/core/src/main/java/org/apache/struts/validator/FieldChecks.java

Don Brown added a comment - 07/May/06 12:34 PM
I think it is a valid change to allow strings with just spaces, as your regex might allow for that. I made the change, so please close the ticket if you can verify.

Don Brown made changes - 07/May/06 12:34 PM
Assignee Don Brown [ mrdon ]
Resolution Fixed [ 1 ]
Fix Version/s 1.3.3 [ 21710 ]
Status Open [ 1 ] Resolved [ 5 ]
Jorge Rodríguez added a comment - 09/May/06 10:13 PM
Thanks for your help, I Hope see this change included in the next release.

Jorge Rodríguez made changes - 09/May/06 10:13 PM
Status Resolved [ 5 ] Closed [ 6 ]
Jeff Turner made changes - 01/Feb/10 01:03 AM
Project Import Mon Feb 01 01:03:21 UTC 2010 [ 1264986201992 ]