Groovy/Grails default error message - grails

I'm trying to customize my errors mesages. Firstly, I want to get message key on output as an error, for example: package.Subpage.title.nullable.
To gain this goal I've deleted all message.properties bundles from my application but I still get something like this on output:
Property [title] of class [class package.Subpage] cannot be blank
I've got properties of FieldError class to see if it's implemented there and I've noticed that this class has property:
defaultMessage=Property [{0}] of class [{1}] cannot be blank
Of course I know that there is a pattern for message key which looks like class.property.(blank or nullable or whatever constraint set) and I can override every message, but my main goal is to have full control on this how message key, message value and default message look like.
Question: Is there any way to define default message and message key for errors in grails?

You can implement your own Spring MessageSource, then wire that into Grails:
MessageSource JavaDoc
Spring Refence - Internationalization using MessageSource
Using the Spring Bean DSL in Grails

Related

Grails 3 use springSecurityService in a trait? Serialization error-

To add a username "lastUpdatedBy" property (along with some other std properties) to several domain classes I created a trait. Everything works fine to the database, but EhCache disk storage gives a NotSerializableException on springSecurityService. I have
def springSecurityService
in order to get the principal.username.
Is there a way to designate the service as transient (static transients=[] doesn't seem to help), or am I at a dead end? Referencing a service from within a domain class is a bit dodgy anyway, but what is a better, workable way to get the username just before saving?
Ok, I figured this out. One of the domain classes had a composite key and was therefore tagged to implement Serializable. Once this was converted to maintain the usual id field and "implements Serializable" was removed, the problem went away.
Just declare your the field as a transient field.
class MyDomain {
// other stuff omitted
transient SpringSecurityService springSecurityService
}
This will do the job when it come to serialization.

using convertEmptyStringsToNull=false only on specific fields?

I have a domain class, which has a composite unique-constraint where some fields are defined "not null".
In this domain class with these fields, I would like to set
convertEmptyStringToNull = false
but only for these 2 fields in this domain-class.
The reason is, that the unique-constraint should work that it take all fields of the constraint into account not only, if all fields are filled...
Is that is somehow possible?
A solution would be, to use the BindUsing annotation from Grails >= 2.3
Example:
#BindUsing({ obj, source -> source['parName2'].trim() })
String parName2
This enables only the trim() function on the field an suppresses the stringToNull functionality.
Here's Grails Jira Ticket with the same problem.
and a discussion here Grails User List
Only problem I have, but it seems not a problem with the solution is, that the duplicate error message mentions now only the first attribute in the composite unique constraint...

Find out which Grails nested field fails to serialize

I'm trying to serialize a Grails domain class instance into Memcached just after updating the domain instance by normal data binding.
When I'm doing this, I'm getting NotSerializableException saying Caused by NotSerializableException: org.codehaus.groovy.grails.web.json.JSONObject which looks like one of the field inside the domain instance is not serializable which is not implementing Serializable interface which is of type JSONObject of Groovy.
But, my domain class does not having such a field. This clearly states that some thing is being set by Grails databinding, since this serialization is working fine if I freshly get a instance.
So, is there any way of knowing that which field is actually failed to serialize?

Spring Security Grails get user details when logging in

I am trying to get more details when I log into my Grails application using Spring Security.
For e.g.:
When I log in using :
http://localhost:8080/XXX/auth/login
I get authentication success and below is the sample response.
{"username":"lalit","roles":["XXX"],"access_token":"something"}
Instead of this, I would also like to return some more user related data like full name.
{"username":"lalit","roles":["XXX"],"access_token":"something","fname":"Lalit Agarwal"}
So, you could see full name also returned as response when I log in.
Note: I have already added a custom field fullName using this : http://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html
The link you noted pretty much outlines exactly what you need to do. You should extend the GormUserDetailsService (or write your own and make sure it implements GrailsUserDetailsService) to make sure that when loadUserByUsername is called, it add the full name to the returned object. Then you just need to add the new bean to your resources.groovy file under the name userDetailsService.

Change the way a DataProperty displayName is looked up in a Validator in Breeze

In the Validator class, the rootContext has a displayName function that is used to find the display name when constructing the validation error message. I'm storing user-friendly display names in the custom properties object of all my DataProperty's, so I'd like that displayName function to look in that area before any other area. As far as I can tell there is no way to override the rootContext from the Validator class in order to change it there. And the only other way I can think to do it would be to pass in a new displayName function in the context when I'm constructing the Validators, but in order to do that I'd have to write a custom metadata parser, unless I'm missing something there. Do you guys have a suggested way of doing this?
It is good question. This is NOT well documented but ... for the time being I would just copy the value resulting from executing your custom displayName function directly onto the 'displayName' property of the corresponding DataProperty or NavigationProperty immediately after querying your metadata. We will be documenting the 'displayName' property in the next release but it is available now.
The subject of improving 'displayName' discovery process is something that is on our plate but we just haven't gotten to it yet.

Resources