Use validate method and validation xml together in Struts 1

ぃ、小莉子 提交于 2019-12-21 22:36:16

问题


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.



来源:https://stackoverflow.com/questions/19949810/use-validate-method-and-validation-xml-together-in-struts-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!