Struts 2
  1. Struts 2
  2. WW-1799

StringLength validator is inconsistent client side and server side

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0.6
    • Fix Version/s: 2.0.7, 2.1.0
    • Component/s: Core Actions
    • Labels:
      None
    • Flags:
      Patch

      Description

      There is an issue with the StringLengthValidator when you submit a blank text field when haven't specified the RequiredStringValidator. Client side the StringLengthValidator is validating (wrong way), Server side it is not validating (right way). It's quite obvious when looking in the generated client side src code.

        Activity

        Hide
        Mads Riise Ladefoged added a comment -

        It seems that this bug has been reintroduced in version 2.0.11

        and there is an addition to the patch:

        <#elseif validator.validatorType = "regex">
        if (field.value != null && !field.value.match("$

        {validator.expression?js_string}")) { addError(field, error); errors = true; }

        should be changed to:

        <#elseif validator.validatorType = "regex">
        if (field.value != null && field.value.length > 0 && !field.value.match("${validator.expression?js_string}

        "))

        { addError(field, error); errors = true; }
        Show
        Mads Riise Ladefoged added a comment - It seems that this bug has been reintroduced in version 2.0.11 and there is an addition to the patch: <#elseif validator.validatorType = "regex"> if (field.value != null && !field.value.match("$ {validator.expression?js_string}")) { addError(field, error); errors = true; } should be changed to: <#elseif validator.validatorType = "regex"> if (field.value != null && field.value.length > 0 && !field.value.match("${validator.expression?js_string} ")) { addError(field, error); errors = true; }
        Hide
        Ted Husted added a comment -

        Completed: At revision: 520235, 520236

        Show
        Ted Husted added a comment - Completed: At revision: 520235, 520236
        Hide
        Mads Riise Ladefoged added a comment -

        Correction to the patch (check is moved to after trim):

        <#elseif validator.validatorType = "stringlength">
        if (field.value != null) {
        var value = field.value;
        <#if validator.trim>
        //trim field value
        while (value.substring(0,1) == ' ')
        value = value.substring(1, value.length);
        while (value.substring(value.length-1, value.length) == ' ')
        value = value.substring(0, value.length-1);
        </#if>
        if(value.length > 0 && (
        ($

        {validator.minLength} > -1 && value.length < ${validator.minLength}

        ) ||
        ($

        {validator.maxLength} > -1 && value.length > ${validator.maxLength}

        )
        ))

        { addError(field, error); errors = true; }

        }

        Show
        Mads Riise Ladefoged added a comment - Correction to the patch (check is moved to after trim): <#elseif validator.validatorType = "stringlength"> if (field.value != null) { var value = field.value; <#if validator.trim> //trim field value while (value.substring(0,1) == ' ') value = value.substring(1, value.length); while (value.substring(value.length-1, value.length) == ' ') value = value.substring(0, value.length-1); </#if> if(value.length > 0 && ( ($ {validator.minLength} > -1 && value.length < ${validator.minLength} ) || ($ {validator.maxLength} > -1 && value.length > ${validator.maxLength} ) )) { addError(field, error); errors = true; } }
        Hide
        Mads Riise Ladefoged added a comment -

        Patch (for file /template/xhtml/form-close-validate.ftl - "&& field.value.length > 0" is added in second line):

        <#elseif validator.validatorType = "stringlength">
        if (field.value != null && field.value.length > 0) {
        var value = field.value;
        <#if validator.trim>
        //trim field value
        while (value.substring(0,1) == ' ')
        value = value.substring(1, value.length);
        while (value.substring(value.length-1, value.length) == ' ')
        value = value.substring(0, value.length-1);
        </#if>
        if(($

        {validator.minLength} > -1 && value.length < ${validator.minLength}

        ) ||
        ($

        {validator.maxLength} > -1 && value.length > ${validator.maxLength}

        ))

        { addError(field, error); errors = true; }

        }

        Show
        Mads Riise Ladefoged added a comment - Patch (for file /template/xhtml/form-close-validate.ftl - "&& field.value.length > 0" is added in second line): <#elseif validator.validatorType = "stringlength"> if (field.value != null && field.value.length > 0) { var value = field.value; <#if validator.trim> //trim field value while (value.substring(0,1) == ' ') value = value.substring(1, value.length); while (value.substring(value.length-1, value.length) == ' ') value = value.substring(0, value.length-1); </#if> if(($ {validator.minLength} > -1 && value.length < ${validator.minLength} ) || ($ {validator.maxLength} > -1 && value.length > ${validator.maxLength} )) { addError(field, error); errors = true; } }
        Hide
        Ted Husted added a comment -

        Can you suggest a patch?

        Show
        Ted Husted added a comment - Can you suggest a patch?

          People

          • Assignee:
            Ted Husted
            Reporter:
            Mads Riise Ladefoged
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development