toriton
Sep 1st, 2010, 06:58 AM
hi all, i'm a newbie about Grails and i have a question about how Grails manage transactions while updating an object domain.
This is my case:
Domain class:
Customer {
String name
String code
static constraints = {
name(nullable:false,blank:false,unique:true)
code(nullable:false,blank:false,unique: true)
}
}
In the Customer controller i have this code for update the Customer domain :
def update = {
Customer customer = Customer.get(params.id)
customer.properties = params;
//here validating the code with my regex or something
if(!isValidCode(params.code)){
customer.errors.rejectValue 'code', 'Bad code'
render view:"edit",model:[customer:customer]
return;
}
if(customer.save()) {
redirect action:list
}else {
render view:"edit",model:[customer:customer]
}
}
Ok the problem is :
Why if i enter in the block if(!isValidCode(params.code)) , that mean the code have a not allowed value , after the return statement the instance is stored with the wrong value on the DB?
How can i mark the instance object retrieved via domainClass.get(id) as not qualified to be saved on the DB?
I use the domainClass.errors.rejectValue('nameProperty','err or message') and i thought that the Framework should recognize as the object is not safe to be saved/updated on the db, but seem that it ignore my errors.
I have the workaround:
discard() on the instance and then merge() instead of save() but what i want know is which is the correct way to accomplish an update without the discard() method. how to set an error in a correct manner so Grails will not save my instance on DB.
Thankin advance for any tip and reply.
T.
This is my case:
Domain class:
Customer {
String name
String code
static constraints = {
name(nullable:false,blank:false,unique:true)
code(nullable:false,blank:false,unique: true)
}
}
In the Customer controller i have this code for update the Customer domain :
def update = {
Customer customer = Customer.get(params.id)
customer.properties = params;
//here validating the code with my regex or something
if(!isValidCode(params.code)){
customer.errors.rejectValue 'code', 'Bad code'
render view:"edit",model:[customer:customer]
return;
}
if(customer.save()) {
redirect action:list
}else {
render view:"edit",model:[customer:customer]
}
}
Ok the problem is :
Why if i enter in the block if(!isValidCode(params.code)) , that mean the code have a not allowed value , after the return statement the instance is stored with the wrong value on the DB?
How can i mark the instance object retrieved via domainClass.get(id) as not qualified to be saved on the DB?
I use the domainClass.errors.rejectValue('nameProperty','err or message') and i thought that the Framework should recognize as the object is not safe to be saved/updated on the db, but seem that it ignore my errors.
I have the workaround:
discard() on the instance and then merge() instead of save() but what i want know is which is the correct way to accomplish an update without the discard() method. how to set an error in a correct manner so Grails will not save my instance on DB.
Thankin advance for any tip and reply.
T.