Grails Problem with custom error messages

后端 未结 5 819
庸人自扰
庸人自扰 2021-02-05 16:30

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 t

相关标签:
5条回答
  • 2021-02-05 17:07

    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}]

    0 讨论(0)
  • 2021-02-05 17:07

    I use fully qualified class names in my messages.properties

    com.shareyourlove.User.password.blank=Some custom message
    
    0 讨论(0)
  • 2021-02-05 17:21

    This worked for me

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

    instead of

    com.model.Customer.name.blank = Custom message
    
    0 讨论(0)
  • 2021-02-05 17:24

    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.

    0 讨论(0)
  • 2021-02-05 17:28

    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

    0 讨论(0)
提交回复
热议问题