Search code examples
javavalidationstrutsstruts-1

Struts Validation Framework not working


I am trying to validate a form using struts validation framework, but the validation seems to be getting bypassed. I have all the validations setup in validation.xml.

I also have some custom form validations in the form's validate() method which are running fine.

I have set the validate="false" in the action mapping and calling the form's validate explicitly from the action's doPerform()

I am on Struts 1.

Here's some code:

Inside the action

if(validationPassed(changeForm, mapping, request)){
 if( !(changeForm.getResultAction().equals("add")) )
   if(!businessValidationsPassed(changeForm, request))
     errorFlag = true;
   }
   else{
        errorFlag = true;
   }

private boolean validationPassed(MultiChangeForm form, ActionMapping mapping, HttpServletRequest request ) {

  ActionErrors errors =  form.validate(mapping, request);
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        return false;
    }
    else
        return true;

}

struts-config.xml

<action path="/multiController"
            type="com.multi.action.MultiControllerAction"
            name="MultiChangeForm"  
            scope="session"
            input="multiUpdateResult.def"
            validate="false">
   <forward name="success" path="/forward/initProcess/cam/multiUpdateResultDef.do"/> 
        <forward name="failure" path="/forward/initProcess/cam/multiUpdateResultDef.do"/>
        <forward name="error" path="/forward/initProcess/cam/errorDef.do"/>

</action>

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

form's validate()

ActionErrors errors =  super.validate(actionMapping, request);
// custom validations: running ok

Am I missing something over here? Any ideas would be really great.

Thanks

Sahil


Solution

  • When validation of the form fails, you are sent to the input page specified in the <action> tag, so I don't know why you are calling the validate method manually.

    Anyways.... what I suspect is that your MultiChangeForm form is extending ActionForm when in fact it should extend ValidatorForm.

    Are you extending ValidatorForm?