PDA

View Full Version : Bind errors


pak
Sep 27th, 2004, 06:56 AM
Hi there

how do I add "bind exceptions" in my OnSubmit method?

I override
ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
and under some circumstances I want to generate an error for a specific field, for showing on the screen - in the same fashion as Spring automatically generates in the <spring:bind> tag.

Thanks,
Peter

katentim
Sep 27th, 2004, 07:22 AM
The usual place is in the validator class associated with the form.

If you must reject a field in the onSubmit method, try:

errors.rejectValue(fieldName, errorCode, errorArgs, defaultMessage);

sds
Sep 28th, 2004, 08:02 AM
No, that won't be sufficient. The errors.rejectValue(..) won't affect the flow. Apparently, once the validation has been done, the errors aren't looked after any more. We subclassed SimpleFormController and overrided processFormSubmission into:


protected ModelAndView processFormSubmission(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
if (errors.hasErrors() || isFormChangeRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
}
else {
logger.debug("No errors -> processing submit");
ModelAndView modelAndView = onSubmit(request, response, command, errors);
// New code: look if the onSubmit has errors
if (!errors.hasErrors()) {
return modelAndView;
} else {
if (logger.isDebugEnabled()) {
logger.debug("onSubmit errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
}
}
}


Perhaps this should be included in the Spring framework, as business validation can happen in the onSubmit.

katentim
Sep 28th, 2004, 06:42 PM
Strange - I tried this before posting just to check and it works fine for me.

rrsIPOV
Sep 28th, 2004, 11:01 PM
I ended up (using a SimpleFormController subclass) with the following:


protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception{
...
if (errorCond) {
errors.rejectValue("fieldName", "code.value",
new String[] {"Substitution argument 1", "Substitution argument 1"}, "Default error message.");

Map model = errors.getModel();
model.put("somethingElse", somethingElse);
return new ModelAndView(this.getFormView(), model);
}
...
return new ModelAndView(this.getSuccessView());
}
[/code]

Colin Sampaleanu
Sep 29th, 2004, 05:56 PM
It can be done in onSubmit as per the code example before this post, however, instead of doing

return new ModelAndView(this.getFormView(), model);

you are generally better off calling showForm(), since if the controller is used with 'sessionForm=true' only the latter will take care of putting the form back into the session. If you just return the ModelAndView with the form view, the form will display fine, but it won't have been put into the session, and when you POST back in, the whole thing will be created from scratch (i.e. there will be a new command object created, as if sessionForm=false was set). Probably not what you want or you would not be using session forms in the first place.

I would argue that Spring itself could detect that the specific form view is being returned and take care of things...