Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails define custom error message for command object

I am writing a Grails (2.3.3 currently) application and have created a validateable command object similar to the following:

@Validateable
class MyCustomCommand {
  String name

  static constraints = {
    name blank: false
  }
}

In my i18n/messages.properties file I defined the following properties to override the default error messages.

MyCustomCommand.name.blank=Name must be provided.
MyCustomCommand.name.null=Name must be provided.

Which per the Grails documentation should be of the format [Class Name].[Property Name].[Constraint Code] as I have done. When I run my application if I leave the value blank I still get the default message for a null property.

I also tried following the example of the default messages and defining them a follows, but still get the default message.

MyCustomCommand.name.blank.message=Name must be provided.
MyCustomCommand.name.null.message=Name must be provided.

I am assuming that I am missing something simple here, but have yet to stumble upon what. Any suggestions on what I am doing incorrectly?

like image 685
Michael Avatar asked Aug 13 '26 22:08

Michael


2 Answers

It is simple indeed. Message should look like:

myCustomCommand.name.blank=Name must be provided.
myCustomCommand.name.nullable=Name must be provided.


//className.propertyName.blank (camelCase with first letter of class name lower)
like image 166
dmahapatro Avatar answered Aug 16 '26 02:08

dmahapatro


So, as I anticipated it was something simple. I was using the defaults as an example which used null where as what I really needed was nullable. Which does make sense as that matches the constraint name.

Therefore the correct version is:

myCustomCommand.name.blank=Name must be provided.
myCustomCommand.name.nullable=Name must be provided.
like image 35
Michael Avatar answered Aug 16 '26 01:08

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!