Struts 2.5.12 / 2.5.13 - Validations stopped working after struts upgrade

982 views Asked by At

I have upgraded Struts on one of the project from 2.3.x to 2.5.12 (tried with 2.5.13 also) and I see that after this upgrade no validations happening.

I have in JSP:

<s:form action="details">
    <s:textfield name="fullName" size="20" label="full.name" requiredLabel="true" />
    <s:checkbox id="terms" name="terms" requiredLabel="true" />
    <s:submit name="submit" id="submit" value="Submit" />
</s:form>

Action name is TestStrutsAction.java. My validation xml TestStrutsAction-validation.xml looks like:

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="fullName">
        <field-validator type="requiredstring">
            <message key="fullname.error"/>
        </field-validator>
        <field-validator type="regex">
           <param name="expression"><![CDATA[^[0-9A-Za-z&\-./\s]*$]]></param> 
           <message key="fullname.invalid"/>
        </field-validator>
    </field>

     <field name="terms">
        <field-validator type="fieldexpression">
            <param name="expression">terms eq true</param>
            <message key="terms.error"/>
        </field-validator>
    </field>
</validators>

I have overridden validate in my action class as:

@Override
public void validate() {
    super.validate();
    LOG.debug(getFieldErrors());
}

I tried debugging and I do not see any field errors in the validate method of action class.

Does anyone know why I have this issue after upgrade? Thanks

1

There are 1 answers

1
Prasann On

The issue was with the regular expression used in the validation xml file. I have a regex in validation xml as:

<field-validator type="regex">
   <param name="expression"><![CDATA[^[0-9A-Za-z&\-./\s]*$]]></param> 
   <message key="fullname.invalid"/>
</field-validator>

I changed expression to regexExpression as below and it started working.

<field-validator type="regex">
   <param name="regexExpression"><![CDATA[^[0-9A-Za-z&\-./\s]*$]]></param> 
   <message key="fullname.invalid"/>
</field-validator>

Not sure why other validations didn't work with the wrong regex, but after changing it all the validations are working.

This might help someone who may have similar issue :)