Show all Spring form errors on Velocity template
I've got an application using Spring MVC and Velocity. On one of my forms, I want to show all errors related to the form at the top of the page. I've figured out how to show errors related to one particular field (using the #springShowErrors macro), but I really want to have one large block of errors at the top of the form, instead of listing the errors next to each individual item.
I've done quite a bit of googling, and a few people have suggested something like
#if ($status && $status.errors.hasErrors())
#f开发者_开发知识库oreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end
...but this gives me no output when placed just below the initial #springBind macro that attaches my command object to the form. Putting #springShowErrors just after the #springFormInput macro for each field works fine, so I know my validator is running and generating errors.
Any ideas? Have I missed something really silly?
Here's the complete form, with my non-working attempt just after the first #springBind
<form name="standardForm" id="standardForm" method="post" action="#springUrl("/requestAccess")">
#springBind("accessRequest")
#if ($status && $status.errors.hasErrors())
#foreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end
<fieldset>
<label for="name">Name</label>
#springFormInput("accessRequest.name" " ")
<label for="company">Company</label>
#springFormInput("accessRequest.company" " ")
<label for="title">Title</label>
#springFormInput("accessRequest.title" " ")
<label for="email">Email</label>
#springFormInput("accessRequest.email" " ")
<button type="submit" value="send">Send</button>
</fieldset>
</form>
Thanks for any help or advice!
I think you're on the right track. There's no direct way I know of to get all the object error messages and field error messages in one aggregated list, however you can do this:
#springBind("bindName")
#if($status.errors.hasErrors())
## Global error messages
#foreach($e in $status.errorMessages)
<p>${e}</p>
#end
## Field error messages
#foreach($f in $status.errors.fieldErrors)
#springBind("bindName.${f.field}")
#foreach($e in $status.errorMessages)
<p>${e}</p>
#end
#end
#end
Not so clean, but it works.
精彩评论