Return Default Value on BindResult Error
Hello,
I am working on an app which has a controller (configured via annotations). One feature I am trying to do is that if a user leaves a field blank then I catch it in my validator, register the error and then set a default value in the backing object based upon some data in another property of the backing object.
I am currently trying this:
Note - returnVal is the result of a call which is determined here to be a binding result. The return val will have a field error in name if the user left the name blank.
<code>
BindingResult br = (BindingResult) returnVal;
if (br.getFieldError("name") != null) {
ModelAndView mav = new ModelAndView();
mav.getModel().putAll(br.getModel());
logger.debug("error in name");
TransformationExperiment mavTE = (TransformationExperiment) mav.getModel().get("transformationExperiment");
if (mavTE.getTransformation() != null) {
mavTE.setName("EXP-" + mavTE.getTransformation().getTransformationId());
} else {
mavTE.setName("EXP");
}
logger.debug("name set to - " + ((TransformationExperiment) mav.getModel().get("transformationExperiment")).ge tName());
returnVal = mav;
}
</code>
However, this does not work. Any values which were bound from the original form (as in html form) appear in the new form. However the name which have just set and added into the ModelAndView does not change.
I have also tried changing the value passed into the method via a @ModelAttribute and also trying to change the value in the session attribute (as this needs to store the value between the GET and the POST I have @SessionAttributes("transformationExperiment") at the top of my class). These have not worked either. I have also tried hacking the value into my validator (bad practice - didn't work anyway).
Has anyone done this, if so could you please give some tips as how to achieve this task?
Thanks, in advance, for your help.
Cheers,
Neil
Default Value on Binding Error
Hello,
That is quite funny because I was refactoring that method to do similar things to which you have suggested - I pulled this version in because it is all one method and therefore a bit easier to read! However, thanks for the tips - I had missed declaring the string array as a parameter as I overlooked that a foreach loop will simply skip if the item being iterated over is null (which is why I missed out the @RequestParam in the first place). Anyways to elucidate and fill in missing gaps, the super method is below:
Code:
public Object onSubmit(TransformationExperiment transformationExperiment,
BindingResult result, String cancel, String delete) {
if (cancel != null) {
logger.debug("cancel");
return AbstractTransformationExperimentFormController.CANCEL_REDIRECT;
}
if (delete != null) {
logger.debug("delete");
transformationExperimentService.delete(transformationExperiment);
return AbstractTransformationExperimentFormController.DELETE_REDIRECT;
}
transformationExperimentValidator.validate(transformationExperiment,
result);
if (result.hasErrors()) {
logger.debug("returning errors");
logger.debug(result.getModel());
return result;
}
logger.debug("success");
transformationExperimentService.saveOrUpdate(transformationExperiment);
return AbstractTransformationExperimentFormController.SUCCESS_REDIRECT;
}
So, (SUCCESS|CANCEL|DELETE)_REDIRECT are strings of the format redirect:/listTransformationExperiments.html. If this is returned then the onSubmit method will redirect, typically if I return a binding result then the errors are shown and the user is redirected to the same view they are on with the errors rendered. However in this case, I need to modify teh backing object so I detect if the super (through the validator) returns a binding result - if it did then I get the contents out of the binding result, modify the backing object and then return a ModelAndView with no explicit view (so it stays on the same page). This does render the errors and the log statement in the jsp correctly logs the modified backing object via the log:debug call. However, the form tag in spring does not pick this up. Interestingly, only the name is not picked up - other properties such as description are showing up. I am thinking that this could be a thing to do with the errors, the error is in the name field and it is the name field which is giving me problems.
Cheers,
Neil