Grails Problem with custom error messages
Asked Answered
E

5

13

I am currently trying to specify custom error messages in grails for the default constraints but so far all I get back is the default error message.

I know that I have to edit the grails-app/i18n/messages.properties file

If I change the following default error codes message, it will correctly display the new error message

default.blank.message=Property [{0}] of class [{1}] cannot be blank

However, this is not what I am trying to do. I need more granular error reporting and have more than one field that can be blank etc. What I would like to be able to do would be, display custom messages for each field in a class

package com.mycompany.myapp

class Test{

 String name
 def constraints = {
 name(nullable:false, blank:false)
 }
}

(following codes appended to end of messages.properties)

test.name.blank=Name cannot be blank
test.name.nullable=Name cannot be nullable

According to the grails documentation this should work correctly, either with or without the package name - className.propertyName.blank

grails.org/doc/latest/ (constraints section) & (section 7.4 - validation & internationalization)

I have tried all comnbinations that I can think of, but it always displays the custom message

I have also tried installing the grails i18n templates plugin

http://www.grails.org/I18n+Templates+Plugin

which generated the error codes automatically for me. I appended the new error codes to the end of the existing messages.properties file but I still get the default error messages.

However, there was something different with the error codes that were generated by the plugin.

instead of the format specified in the grails doc - test.name.null=......, it automatically generated test.name.null.error=Custom Message

I have also tried deleting the default error messages completely, but they are still displayed

If anyone has experienced this issue before, I would appreciate any help that anyone can give me

Thanks in advance

Ethnomusicology answered 14/7, 2010 at 21:25 Comment(3)
I've created a detailed answer on how to create a custom validation and post a custom error message back to your view here: #14039405Anselm
would making int test.name.blank.message change anything for good?Inheritable
This saved my day: ((MessageSource) Holders.getGrailsApplication().getMainContext().getBean("mes‌​sageSource")).getMes‌​sage(e.getErrors().g‌​etAllErrors().get(0)‌​, LocaleContextHolder.getLocale());Eolic
E
18

put def messageSource (in controller or service)

item.errors?.allErrors?.each{ 
println  messageSource.getMessage(it, null)
};

I also found a good link which explains this better

http://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html

Ethnomusicology answered 16/7, 2010 at 23:2 Comment(1)
println message(error: it) seemed to work a little better for me. This shortcut method is referenced in the comments on the linked blog.Noria
M
8

Well, the documentation shows you an example of how to override the messages for one of the default Validation Constraints (blank, nullable, min, max, size, range, etc.). But it fails to tell you to look in the documentation for each Constraint and at the bottom it shows you what propery key to use:

Error Code: className.propertyName.size.toosmall or className.propertyName.size.toobig

for Constraint size http://grails.org/doc/latest/ref/Constraints/size.html

So, for

package com.example
class User {
    String username

    static constraints = {
        username  size:5..15
    }
}

use:

com.example.User.username.size.toosmall=Yo there! too small: [{0}] of class [{1}] with value [{2}] does not fall within the valid size range from [{3}] to [{4}]

com.example.User.username.size.toobig=Yo there! too big: [{0}] of class [{1}] with value [{2}] does not fall within the valid size range from [{3}] to [{4}]

Monoatomic answered 4/12, 2013 at 21:41 Comment(1)
You're a lifesaver my friend. God forbid they document any of this in the user guide.Panacea
S
0

It might be that your constraints aren't static - it should be specified as "static constraints = { ..."

Also note that nullable defaults to false so you don't need to specify that.

Snippy answered 14/7, 2010 at 22:42 Comment(1)
Sorry, that was a mistake in the example above. I am actually using static constraints in my real code. Thanks for the nullable information The updated example would be package com.mycompany.myapp class Test{ String name static constraints = { name(blank:false) } }Ethnomusicology
T
0

I use fully qualified class names in my messages.properties

com.shareyourlove.User.password.blank=Some custom message
Thecla answered 15/7, 2010 at 6:8 Comment(2)
Thanks for the suggestion, I have already tried this and any combination that I can think of. The grails doc (section 7.4) also specifies that "The class name is looked for both with and without a package, with the packaged version taking precedence." So if the documentation is correct, it shouldnt matter whether the class name is specified or not. The i18n plugin also generates the error codes without the package name, except it would be in this format: test.name.blank.error=Name cannot be blankEthnomusicology
I have managed to create a new app which uses scaffolding and get the custom error messages returned to the web interface, however, the error messages that are written to the console/error log still display the default error messages. And I am still not sure what is wrong with my existing applicationEthnomusicology
G
0

This worked for me

com.model.Customer.name.nullable.error = Custom message

instead of

com.model.Customer.name.blank = Custom message
Gurkha answered 24/3, 2016 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.