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.
Related
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.
I did a Google search but came up dry on anything that explained what is happening...
I have the following domain class:
class Company {
Integer companyId
String name
static constraints = {
companyId unique: true
name blank: false
}
String toString(){
return name
}
}
When I do a 'new Company(params)' in the controller with the passed in values, it returns an object with the 'companyId' field set to null. I've double checked the values in 'params' and both 'params.name' and 'params.companyId' are set to non-null values.
What am I doing wrong?
I have another domain/controller (created by someone else now gone) doing something similar that works fine so I must be missing something.
I'm new to Grails/Groovy and any/all advice welcome.
Lets say I have a Person class. Person has two children Builder and Doctor.
The Person class comes with username attribute (as they often do). By inheritance of course the Builder and Doctor classes also have a username.
Now I want to beautify the i18n validation messages. I add the following thinking it will apply to all 3 of my classes
person.email.blank=Email is required
but it is not being used when validating objects of the child classes. If I add the following
doctor.email.blank=Email is required
builder.email.blank=Builder is required
I get the desired result, but it doesn't seem to be to be as DRY as it could be.
Does anyone have any thoughts on this. Maybe I'm just doing something wrong!
I was able to achieve this by using custom validator and a single message for all required message.
In the properties file:
default.isRequired= {3} is required
email=Email
otherProperty=Other property
in domain class:
email validator: {val, obj -> obj.isRequiredValidator(val, "email") }
email validator: {val, obj -> obj.isRequiredValidator(val, "otherProperty") }
private isRequiredValidator(val, key) {
if (!val)
return [default.isRequired, messageSource.getKey(key)]
}
Result after calling validate() will be "Email is required" and "Other property is required"
You can reuse the custom validator method by sharing it between domain classes.
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.
There is domain class with natural key defined as below
class InterestGroup {
String intGrp
String name
static constraints = {
intGrp(blank: false, maxSize: 4, unique: true)
name(blank: false, minSize: 10, maxSize: 50)
}
static mapping = {
id generator: "assigned", name: "intGrp", type: 'string'
}
String toString() { "${intGrp}"}
}
I try to modify standard scaffolding to make possible changes of name field.
In standard code there is save() method called and it checks all field, and of course record could not be updated because record with same key exists. When i just assign field value
interestGroupInstance.name = params?.name
name is updated but, not checked against domain class constaint.
What is the best way to realize CRUD operation with natural keys based tables?
Best regards
Krzysiek
I don't think I'm understanding you. What are you trying to do? You are trying to update your group's name and it doesn't seem to make any validation?
The reference documentaion says: "The save method informs the persistence context that an instance should be saved or updated. The save method returns null if validation failed and the instance was not saved and the instance itself if successful.". So it should be calling validate methos when you call save methos on your domain class object.
Could you please post an example?