javavalidationconfigurationstruts2struts-validation

Struts2 form-fields validation does not work


I'm trying to implement the validation as described here (struts2 documentation). My Class :

public class FatturaAction extends BaseAction {

    private static final long serialVersionUID = 6586322371651933659L;
    private FatturaForm fatturaForm;

    //getter and setter
}

The FatturaForm:

public class FatturaForm {

    private Date data;

    //getter and setter
}

My XML (FatturaAction-validaton.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <field name="fatturaForm.data"> 
        <field-validator type="required"> 
            <message>You cannot leave the email address field empty.</message>
        </field-validator>
    </field>
</validators>

The validation is not hit, even if the field is empty. I'm using <interceptor-ref name="defaultStack" /> so the validation interceptor is activated.

EDIT:

I also tried the non field validator:

<validators>
    <validator type="required"> 
        <param name="fieldName">data</param> 
        <message>You must enter a value for bar.</message> 
    </validator>
</validators>

It doesn't hit either.


Solution

  • You're using a wrong DTD:

    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" 
              "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
    

    the right one for latest version of Struts 2 is

    <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
              "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    

    Also ensure your FatturaAction-validaton.xml is in the action folder, NOT in the bean folder (that's right only for Visitor Validation)