views:

427

answers:

1

Hi,

I have a form

<form:form commandName="command" method="post">
    <form:errors path="property"/>
    // some fields
</form:form>

And a MultiActionController

Command command = new Command();

ServletRequestDataBinder binder = createBinder(request, command);

binder.bind(request);

for (Validator validator: getValidators())
    if(validator.supports(command.getClass()))
        validator.validate(command, binder.getBindingResult());

if(binder.getBindingResult().hasErrors())
    // What to put here in order to show error message in <form:errors path="property"/>

Question just above. I have used

return new ModelAndView()
                     .addObject(binder.getBindingResult().getModel()) 
                     .addObject("command", command);

But does not work fine. When the form is shown again (after a failure) command properties is shown but error messages not

correct answer: instead of

return new ModelAndView()
                     .addObject(binder.getBindingResult().getModel()) 
                     .addObject("command", command);

I have to call

return new ModelAndView()
                     .addAllObjects(binder.getBindingResult().getModel());

Notice addAllObjects. Now it works fine!

regards,

+1  A: 

Umm... it's too late at this point.

binder.getBindingResult() is an instance of BindingResult which is a subclass of Errors. Errors produced by both your bind() method invocation and your validators have already been added to that BindingResult instance.

If some of them happened to be associated with property path you'll see them in output of <form:errors>, otherwise you won't. You could, of course, iterate through them and re-add them back with the path in question but what would be the point?

ChssPly76
Hi Chss, So what to do in order to show form:errors
Arthur Ronald F D Garcia
Take care is a MultiActionController
Arthur Ronald F D Garcia
Chss, when i call binder.bind Validators is not called. I have used a logger inside it. When i call validator logger prints to output.
Arthur Ronald F D Garcia
`bind()` is not going to invoke validators for you but you do (your code above looks fine). My point is that you're not likely to see anything as a result of `<form:errors path="property"/>` unless either bind fails for it (does it?) or one of the validators explicitly specifies that as path (does it?) Did you try leaving 'path' out altogether to see all the errors?
ChssPly76
Hi Chss, code is right. See additional comment in my question
Arthur Ronald F D Garcia
binder.bind(request); does not performs validation. I take a look at source code.
Arthur Ronald F D Garcia