1

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

Prasann
  • 1,263
  • 2
  • 11
  • 18
  • *do not see any field errors in the validate method of action class* - What do you mean by that? Does it goes to the input result? – Aleksandr M Oct 30 '17 at 19:14
  • Hi @AleksandrM, I have overridden the `validate` method in my action class just to log the errors. On submit from the jsp page, I see that control comes to validate method, but there are no field errors added. I have updated my question to add the `validate` method. Thanks for your response. – Prasann Oct 30 '17 at 20:43
  • If you look at [my answer](https://stackoverflow.com/a/18504177/573032), you will find that validate method comes right after the interceptor and if it validates without errors then what field errors did you expect? – Roman C Oct 30 '17 at 22:44
  • Hi @AleksandrM, I am expecting a few field errors (through XML validation) as I am not entering any values on screen. In your answer, you have mentioned _The validation interceptor does the validation itself and creates a list of field-specific errors._ - I do not see any field validations created by validation interceptor, though I have included the validation interceptor in my struts xml file. – Prasann Oct 31 '17 at 09:35

1 Answers1

1

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 :)

Prasann
  • 1,263
  • 2
  • 11
  • 18