Use validate method and validation xml together in

2020-07-24 04:46发布

问题:

I am unable to use validations in validate method and validations in validation.xml together , If I comment the validate() method then form validation.xml validation is working, else only the validations done in validate method alone is working!

I am pasting the excerpts of code involved below, please do let me know on valuable suggestions:

public class DvaUpdateBean extends ValidatorActionForm implements Serializable {

//getter setters

public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    String method = request.getParameter("method");
    if (method == null){
        return errors;
    }

    if(method.equalsIgnoreCase("updateDvar")){
        if (getDescription() == null || getDescription().length() < 1) {
            errors.add("descreq", new ActionMessage("error.ps.descreq"));
        }

        if (getReason() == null || getReason().length() < 1) {
            errors
                    .add("reasonreqd", new ActionMessage(
                            "error.ps.reasonreqd"));
        }

        if( getInVoiceRadio() == null || getInVoiceRadio().length() < 1 ) {
            errors.add("invreq",new ActionMessage("error.dva.invreq"));
        }


        if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("Y") &&  ( getInVoiceNumber()==null || getInVoiceNumber().length() < 1) ) {
            errors.add("invnumreq",new ActionMessage("error.dva.invnumreq"));
            //if ( getCrDate()!=null && getCrDate().length()<1) {
                //errors.add("condatereq", new ActionMessage("error.wlrr.condatereq"));
            //}
        }

        if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("Y") &&  ( getInVoiceNumber()!=null || getInVoiceNumber().length() > 1) && ( getInVoiceDate()==null || getInVoiceDate().length()<1 ) ) {
            errors.add("invdatereq", new ActionMessage("error.dva.invdatereq"));
        }

        if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("N") &&  ( ( getInVoiceNumber()!=null && getInVoiceNumber().length() > 1) || ( getInVoiceDate()!=null && getInVoiceDate().length()>1 ) ) ) {
            errors.add("invreqyn", new ActionMessage("error.dva.invreqyn"));
        }   
    }   

    return errors;
}

}

Validation.xml

 <form-validation>
    <formset>
<form name="/dvaSearch">
        <field property="dvaSearchBean.dvasrNumber" depends="integer">
            <msg name="integer" key="label.invalidDvasrNumber" />
        </field>
        <field property="searchOriDate" depends="date">             
            <var><var-name>datePatternStrict</var-name><var-value>MM-dd-yyyy</var-value></var>
            <msg name="date" key="label.invalidOrignDate"/>
        </field>
    </form> 

</formset>
</form-validation>

Struts-config.xml

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>

回答1:

I ran into this same issue ashwinsakthi. Here's how I resolved it.

NOTE: My form bean inherits from ValidatorForm and not ValidatorActionForm like in your code. ValidatorActionForm inherits from ValidatorForm so I think it'll work.

The idea is to call the parent validate method from inside your form. You'll get the errors, if any, from the validator framework first that way. Then you go ahead and add your own validation the normal way.

@Override
public ActionErrors validate(
        ActionMapping mapping,
        HttpServletRequest request) {

    // errors return from validator framework (validation.xml file rules)
    ActionErrors errors = super.validate(mapping, request); 

    // Now check other properties (outside of validation.xml)
    if(somefield == ""  ) {
        errors.add("example", new ActionMessage("some.resource.file.key"));
    }

    ... continue to validate your properties

    return errors;
    }

In my jsp file (where my form is) I include the following for all errors to display after the form is posted:

<font color="red">
<html:errors/>
</font>

Finally here is a snippet from my struts-config.xml file.

   <action 
        path="/hellotest"
        type="com.dan.HelloTestAction"
        name="helloTestForm"
        scope="request"
        input="/index.jsp"
        validate="true">
    <forward name="success" path="/result.jsp"/>
    <forward name="failure" path="/index.jsp"/>
   </action>

In my testing I get the expected results you are expecting. Try it out.