Grails domain custom error messages - grails

I am working in a grails application, I have many domains in the applications, more than 50! The error message generated are default, I have the following in my message.properties file
default.blank.message=Field "{0}" cannot be blank.
An example of my one of the domain class is as follows:
class Person {
String firstName;
String lastName;
String middleName;
Date dob;
String gender;
String religion;
String nationality;
String maritalStatus;
Zone permAddZone;
District permAddDistrict;
String permAddVDC;
}
And so on, so when I leave a field, for example 'maritalStatus' the following error message shows up, 'Field "maritialStatus" cannot be blank.'. I also added the following to my message.properties file
person.maritialStatus=Maritial Status
But still I am getting the 'Field "maritialStatus" cannot be blank.' message. I also tried using I18n Templates Plugin, but as I have a lot of domain classes, modifying error message for all of them would be time consuming. So, what choices do I have or what am I doing wrong. Any help is appreciated.

The appropriate message key to override is specified on the reference page for each constraint type. In the case of blank it's
com.example.Person.maritalStatus.blank=Marital status must not be blank
However this will be very time consuming and repetitive if you have many domains and many properties.
What you are asking for is instead a way to process the domain class property names in some way before they are inserted into the default messages. This isn't something Grails supports out of the box but I like a challenge... and I've found a surprisingly elegant way to implement it. My approach is the grails plugin available at https://github.com/ianroberts/recursive-messages and it works by extending the format string syntax to support placeholders of the form
default.blank.message=Field "{0,message,field.name.}" cannot be blank.
A placeholder {N,message,prefix} is resolved by prepending the given prefix to the usual placeholder replacement value and then treating the resulting string (field.name.maritalStatus in this example) as a no-argument message key, and looking that up in the usual way. Thus you could have different representations for different languages.
It has to be a plugin because it relies on a trick that works in a plugin's doWithSpring but doesn't work in an application's resources.groovy, to modify the definition of the default messageSource Spring bean.
Disclaimer: this was a quick fix, it hasn't been fully tested and could probably be implemented more efficiently.

In order to override the field label you need to override the key:
<package>.<domainclass>.<fieldname>.label
So in your case, try:
<package>.Person.martialStatus.label=Label

Related

Custom wrapper for log4j2

I want to build a wrapper around log4j2 to do the below:
1) There are around 6 mandatory fields like event_name, action, desc etc
2) Some fields, i want to make them use only certain values, like enum
3) log should be created in key value pairs for Splunk.
Below is my approach:
1) Created a class called CustomLogger accepeting the mandatory fields, logger and variable fields as key value
2) Users can call methods like below:
CustomLogger.info(logger, transactionId, app_name, event_name,
"inside the loop", "inside the loop of the sample app",
CustomLogger.Result.success, "looped in", "loop_count",
String.valueOf(i));
Method definition:
public static String log(LogLevel logLevel, Logger logger,
String transactionId, String app_name, String event_name,
String action, String desc, Result result, String reason,
String... addtnlFields)
Issues with the approach:
1) Not extending the log4j, not sure if this is the right way
2) need to pass the logger from every class. If that can be avoided
3) method and line number is lost since it is getting called from a different method
This will be widely used across my internal applications, so want to do it right. Is this approach ok or is there a better approach?
Take a look at the code generator attached to this Jira: https://issues.apache.org/jira/browse/LOG4J2-519
Perhaps you can use that as a base class? Gives you a slightly nicer API.
(I still need to update this to reflect some API changes in log4j-2.0-rc2...)
UPDATE
A different approach is to have a custom implementation of the Message interface defined in the log4j2 api module. Your custom message would have a constructor with all fields you define as required, and the toString method (and perhaps some other methods too) would format these fields as you require into key-value pairs.

Hibernatate doesn't bind enums

After update to Grails 2.3.7 from Grails 2.2.4 enums binding in my domains stop working.
I can't figure out what happened, someone could give me a hint?
There may be some specific aspect that is broken and if you can identify that please report a JIRA and we can look at it, but in general enum binding does work in Grails 2.3.7. The simple way to make that work is to have request parameters whose name matches the name of the enum property you are binding to and the value is the String representation of the enum instance. For example, if you have the following...
// grails-app/domain/com/demo/Company.groovy
class Company {
Status companyStatus
// ...
}
// src/groovy/com/demo/Status.groovy
enum Status {
ACTIVE, INACTIVE
}
Then if you bind companyStatus='INACTIVE' or companyStatus='ACTIVE' to a Company object, that should work.
I just created a sample app at https://github.com/jeffbrown/enumbinding which demonstrates that working in Grails 2.3.7. Run the app and submit the form on the default index page to see it in action.
I hope that helps.

Grails adding version to command object causes id and version from params not to be bound

I apologize if I'm missing something really obvious here but I've been pulling my hair out with this issue.
I have a command object:
class MyCommand {
Long id
String value
}
I bind to this in my controller:
public update(MyCommand myCmd) {
}
Everything is fine in this scenario. Now I'm trying to add the version field, which is passed in the request parameters to the command object:
class MyCommand {
Long id
Long version
String value
}
Now however when the binding happens the id and version are always null, even though they are present in the params object.
I suspected that there may be some special handling for id / version attributes related to how grails handles optimistic locking (as this is ultimately why I'm doing this) but the issue is present at the command object independent of any domain object.
I'm baffled why this is not working. Is there some special case when version is present on a command object?
Seems this is by design per Jeff Brown jira
The data binding explicitly avoids binding id or version [if] they both
exist and does this by design. This is a shield against potential
security problems relevant to data binding as it relates to domain
classes. A simple work around for command objects would be to name the
properties with something like "idValue" and "versionValue" or
anything other than "id" and "version".

GORM mapping: make an index unique

I'm feeling a little slow today. I'm trying to do something that I think is very simple. I have a Domain class with a property called 'name'. I want 'name' to have an index, and I want the index to require that the 'name' is unique. I've set the unique constraint and tried creating an index. I can't make sense out of the Gorm docs as to how I add the unique attribute to the index. Here's some code:
class Project {
String name
static hasMany = [things:Things]
static mapping = {
name index:'name_idx'
}
static constraints = {
name(unique:true)
}
}
All is well with the above, except when do "show indexes from project" in mysql it shows my name key as not unique. I know the problem is that I am not specifying unique in the mapping, but quite frankly the docs for gorm are making my head hurt. I see all kinds of stuff about columns, but I can't find a single example anywhere on the web that shows what I want to do. I don't need complex mappings or compound keys, I just want to know the syntax to add the unique attribute to the mapping declaration above. Any advice welcome.
I also did a grails export-schema and see the following:
create index name_idx on project (name);
Nothing in that to indicate this index requires unique values
A related followup question would be once I succeed in making that index unique, what type of error should I expect when I go to save a Project instance and the name is not unique? Is there a specific exception thrown? I realize that even if I check that a given 'name' is unique there's still a possibility that by the time I save it there may be a row with that name.
I'm quite sure the syntax to do what I want is simple but I just can't find a simple example to educate myself with. I've been to this page but it doesn't explain HOW the uniqueness is enforced. I'd like to enforce it at the name index level.
The indexColumn allows additional options to be configured. This may be what you're looking for.
static mapping = {
name indexColumn:[name:'name_idx', unique:true]
}
Grails Documentation for indexColumn
If you put only the unique constraint the GORM send DDL to create an unique index on database.
static constraints = {
name nullable: false, unique: true
}

Grails Problem with custom error messages

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
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
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}]
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.
I use fully qualified class names in my messages.properties
com.shareyourlove.User.password.blank=Some custom message
This worked for me
com.model.Customer.name.nullable.error = Custom message
instead of
com.model.Customer.name.blank = Custom message

Resources