I have a j2ee web application running on struts. I am using validation XMLs for my form validation. I ha开发者_StackOverflow中文版ve a problem with multiple buttons in One form. I have Save, Delete and Cancel buttons. The problem is I want that when the delete button is clicked the validation is not performed. How do I do this.
Haven't used the Validation framework that much with Struts, but you might do this with overwriting the validate() method in your action form.
The org.apache.struts.validator.ValidatorForm is the Struts entry point into the Validator plugin. The ValidatorForm overrides the validate() method of the ActionForm and delegates validation to the Validator.
You might skip validation for your desired button if you overwrite the validate() method in your form and only call the one in ValidatorForm if needed, something like:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
if (/* check for clicked button*/) {
//skip validation and just return empty (i.e. no error)
return new ActionErrors();
}
// else, delegate to the validator framework
super.validate(mapping, request);
}
I was also frustrated by this limitation. I came up with "cancel" type functionality by creating an input button inside the form, with a url tag as the action to set the browser location to. That way the validation on the form will only fire for submit, but not for our special cancel button. I was not able to make it skip validation for one button or the other when both were of type submit
. With this solution the client side validation will work properly as well.
<sx:form validate="true" action="someAction">
<sx:submit value="Save Changes" theme="simple"/> |
<sx:url action="umhome" id="cancel"></sx:url>
<input type="button" value="Cancel" onclick="return window.location = '<sx:property value="%{#cancel}"/>'; "/>
</sx:form>
精彩评论