Using jQuery to give Grails constraint errors a nice appearance (but not client side validation)
On the server, G开发者_如何学Gorails provides constraint validation of the fields that will be saved into domain object / database, and then provides the <g:renderErrors>
tag to display any field errors in the html/form page, generally displayed at the top of the page.
What would be the easiest way to take these server side errors and instead display them next to each field in a red error callout? I.e. what's a simple (jQuery) library to just put an error callout next to a given field, without doing validation in the client?
(Grails 1.3.7, plan to update to 2.0 later) - edited
The hasErrors(bean: bean, field: field, ) tag in the groovy taglib will help you here. As Sergio said, you can call it on your GSP and use it to add css styles to your inputs (or input containers). Here's an example for a view which has a "person" model passed, which has a field called "name" and I want to apply a 'errors' css class:
<td class="${hasErrors(bean: person, field: 'name', 'errors')} text">
As for intelligent, user-friendly messages about "why" a field is invalid, you can add a i18n property code to your Domain validator, corresponding text in your i18n folder, and I believe use something to the effect of renderError to print that message (it should be attached to the error). Then you could lookup the error by the field name, and inject that message into the view. Not 100% sure on how to do that though.
精彩评论