How can I override the default error message in Grails?
Asked Answered
D

2

13

I have a Person object with a lastName field. The lastName field cannot be blank. When the user submits a form blank value in the lastName field, the error message that the user sees is:

Property [lastName] of class [com.example.Person] cannot be blank

This error message is lame. I want to customize it to something more user friendly, like "The Last Name field cannot be blank"

How do I do that?

Damon answered 7/2, 2011 at 14:48 Comment(0)
C
22

See Chapter 7 of the docs: http://grails.org/doc/latest/

You would change grails-app/i18n/messages.properties and add

person.lastName.blank=The Last Name field cannot be blank
Claman answered 7/2, 2011 at 15:16 Comment(6)
Thanks. I'd spent a lot of time trying to track that one down - somehow I'd missed it on that page.Damon
There's also information in each constraint page about what property name it uses - those are in the left frame in the Constraints section. E.g. the creditCard constraint uses 'className.propertyName.creditCard.invalid' - see grails.org/doc/latest/ref/Constraints/creditCard.html for a direct link.Claman
it just don't work for me. It seems like grails version different cause this issue.Bissau
Same here with Grails 2.5.0. I can't override messages from my plugin.Eleanoreleanora
I'm also seeing this issue with Grails 2.5.0 - can't over-ride messages from pluginRoselynroseman
Re: different grails version - Try searching for "Validation and Internationalization" in grails doc for your version.Coth
C
1

In Grails 3 you need to change in grails-app/i18n/messages.properties:

default.blank.message=The {0} field cannot be blank

Cryptomeria answered 28/5, 2017 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.