grails/gorm messages bundle problem - grails

In my grails application i use GORM. I want to customize error messages for each class. Imagine i have this class:
class City {
String name
Region regiao
District district
static belongsTo = District
static constraints = {
regiao(blank: false, nullable:false)
district(blank: false, nullable:false)
name(blank: false, nullable:false, unique: true)
}
String toString(){
name
}
}
i want to customize the error messages in "messages.proprieties".
Imagine i want to make an error message for this class. the default error message for unique is the following:
default.not.unique.message=Property [{0}] of class [{1}] with value [{2}] must be unique
My error message will be something like this: ?
packagename.City.not.unique.message= Must be unique !
Please help, i cant get this to work..
Thx in advanced.

EDIT -- turns out that the answer is in the documentation. Each constraint, in the Constraints section, has the property path to use. So for unique its
className.propertyName.unique
but the path varies according to the specific constraint.

ok so it is it. to make sure how message syntax is just check grails documentation, in constrains section. for each type of constrain, at the end there is the corresponding error message.
For example, go: http://grails.org/doc/latest/
The constrains type 'maxSize' error is the following:
Error Code: className.propertyName.maxSize.exceeded

You want to customise below message.
default.not.unique.message=Property [{0}] of class [{1}] with value [{2}] must be unique
I have tried below code it is working .
city.name.unique.error = city name must be unique.
or
city.name.unique.message = city name must be unique.

Related

Is any package name restrictions applied in grails?

I have created a Domain class called Action under package promotion
package promotion
class Action {
String name
String code
String address
static constraints = {
name blank:false
code maxSize:5
address nullable:false
}
}
Issue: Here my problem is I can not generate controller&views. And also I can not change default error messages from message property for my constraints
example: promotion.Action.name.nullable=Please enter the action name
Note: But if I change my package name "promotion" to something like "com.app.act" it works fine.
Try to make package name little brief. So it can be a good coding practice also

Property [] of class [] can not be null error in Grails

I am new to Grails and getting this error on two out of my three domains when I run the project on all the domains content. The domain the works is
class Location {
def scaffold = true
String company
String name
String address
static belongsTo=[company:Company]
static constraints = {
}
}
The domain that does not work is
class Report {
def scaffold = true
String title
String location
Date published
static belongsTo=[location:Location]
static constraints = {
}
}
I can not see the structural or syntax issue that is trowing the errors. I have been trying a variety of adds and subtracts and can not seem to find anything that address this error at a basic level. Again I have just started using Grails and Groovy
After doing the changes told by #araxn1d, you should also check the constraints. By default all properties are not nullable (that's why you're getting for example the error for the title property)
If you already have data in your database you have two options:
Update your database and set the correct values in each table or row
Set each property in the domain as nullable. For example
static constraints = {
title nullable:true
}
Are you creating a Report with no properties set? By default, Grails will check that all properties are not null. If you want to allow the user to leave a certain field undefined (null), then you have to explicitly tell Grails in the constraints map:
static constraints = {
propertyName nullable: true
}
Seems like error is in this line:
String location
location is String type, but should be Locationtype. The same as String company in Location domain should be Company company.
I think the problem is that you've changed the structure of the domain class after you generated the automatically created views/controller for the domain.
I fixed it by just deleting the offending domain along with the controller/views file and recreating them. I'm sure you could root through and find the offending code though.

How do I change the name of a Grails domain class id field?

I have a Grails 2.2.3 domain class called FundType that I am trying to map to a legacy database table. It has two fields: code and description. I would like the id to be called code anytime I use the domain class and preferably on any of the generated scaffolding. But every time I use the name key on id I get this exception:
| Error 2013-07-24 09:38:44,855 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]: null
Message: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]: null
This is what my domain class consists of:
class FundType {
String id
String description
static mapping = {
id column: 'fund_code', generator: 'assigned', name: 'code'
description column: 'fund_desc'
}
}
And anytime I am using a FundType instance I would like to call code like fundTypeInstance.code and NOT fundTypeInstance.id. This will make it more user friendly for me because I'm dealing with something called code, not id.
So I would like to know is what I'd like to do possible? And what am I doing wrong in my domain class that is causing this ORM mappings error?
Edit:
Okay, so I changed my domain class to the following and I am getting a FundType not found with ID null error.
class FundType {
String code
String description
static mapping = {
id generator: 'assigned', name: 'code'
code column: 'fund_code'
description column: 'fund_desc'
}
}
I added some sql logging to see what Hibernate is doing and this is what was output: select * from ( select this_.FUND_CODE as RTVFTYP1_1_0_, this_.FUND_DESC as RTVFTYP2_1_0_ from RTVFTYP this_ ) where rownum <= ?
Use String code instead of String id in the domain class.
You are deliberately mentioning to the GORM that I want to use the property code which maps to table column fund_code whose value is assigned as the id (primary key). In that case, you just need to have the property codedefined in the domain class instead of the id.
(I'm answering the fix that worked for me for future use by other programmers)
#dmahapatro was right, I needed to add String code.
It looks like naming the id something different just doesn't play well with Grails dynamic scaffolding. I did some tests and I can still use FundType.get(code) and it will return the object just as if I passed in an id. I can also do FundType.findByCode(code).
It looks like I have to change the scaffolded controller to expect a String id instead of the default Long id. I also have to change the scaffolded list view to send fundTypeInstance.code instead of fundTypeInstance.id to the show controller, but I suspect that adding a getId() that just returns this.code will fix that.

How render custom error messages?

I have a domain class :
package x
class User {
transient springSecurityService
String username
String password
//other stuffs.
static constraints = {
username blank: false, unique: true
password blank: false
email blank: false, email:true
}
}
This is the class that I used for Spring security. In my /register/index page I need custom error messages, so I added these lines into message.properties:
x.User.username.unique=Username already exists. Please use other username.
But that doesn't seem to be working. I get only this error message :
Property [{0}] of class [{1}] cannot be null
Even though I pass some value into my username column I still get this error message. I'm confused with it. How this /register/index comes from?
Where I need to change the error message?
Thanks in advance.
The problem is the way you've declarated the message code, it doesn't fallow the convention. Take a look Validation and Internationalization. Here you can find that the right way is [Class Name].[Property Name].[Constraint Code]
So your constraint should be
user.username.unique=Username already exists. Please use other username.

Grails domain ID column validation issue

I have a simple domain object
class MyDomain
{
String id
String name
static constraints =
{
id unique:true
name nullable:true
}
static mapping =
{
table 'schema.MyDomain'
id column:'MY_ID', type:'string', generator:'assigned'
}
}
The issue I am having is when I call validate on the object, it returns true even when the id field is null. I had thought that all columns were nullable:false unless explicitly stated otherwise. If I change the line
id unique:true
to
id unique:true, nullable:false
then it seems to work fine. My main question is, why do I have to explicitly set nullable for the ID column? It is just a small line of code, but I don't like just adding in the tag of code without understanding why in case it is a symptom of a bigger problem.
The id column is auto generated and auto populated(when versioning is turned on[true by default]) and you shouldn't have to declare a new one.
This default id column is nullable:false by default and you can still set the mapping properties and id generation strategies like you have done above against it.
However if you want to define the default constraints for all domain in you app, you can do it globally by setting thwe following in your config.groovy file.
grails.gorm.default.constraints = {
myShared(nullable:true, size:1..20)
}
For more on constraints see the Grails documentation.

Resources