java - Retrieving Grails Domain Validation Messages -
i'm pretty new grails , i'm sure has been asked multiple times. have application i'm developing in grails. in service, make entry multiple tables. if 1 of them fails mysql unique constraint exception, error part of domaininstance.errors object. how parse return appropriate error code controller.
normally in spring, adding exception interceptor controller take care of this. whats preferred way in grails.
when validated domain object has validation errors stored under domainobject.errors
, implementation of spring errors interface. render errors field of object in gsp, typically use like:
<g:haserrors bean="${book}" field="title"> <div class="errors"> <g:rendererrors bean="${book}" field="title" as="list" /> </div> </g:haserrors>
the exact message displayed resolved message*.properties
files. if want these messages in controller instead, this:
class mycontroller { messagesource messagesource localeresolver localeresolver def myaction(book book) { locale locale = localeresolver.resolvelocale(request) if (!book.validate()) { list allerrormessages = book.errors.allerrors.collect { messagesource.getmessage(it, locale) } // print error messages println allerrormessages } } }
Comments
Post a Comment