user1020194
user1020194

Reputation: 1

Grails Domain class property Set failed Validation

I have problem with a setter in grails. I have two properties beforeTax and afterTax. I only want to store one property in the db beforeTax. In the ui I want the user to enter either before or after tax. So I made the afterTax a transient property like this:

double getAfterTax(){
  return beforeTax * tax
}

void setAfterTax(double value){
  beforeTax = value / tax
}

When I now enter the after tax value and want to save the object the validation fails (before tax can not be an empty value)

What am I doing wrong?

Upvotes: 0

Views: 540

Answers (2)

Kjirsten
Kjirsten

Reputation: 11

If I understand your question correctly, you want to compute beforeTax based on the value of afterTax?

You could use the event handler methods beforeXXX() where XXX is Validate, Insert, and Update to compute beforeTax. Then beforeTax's constraint can be nullable:false.

def beforeValidate() {
   computeBeforeTax() 
}

Upvotes: 1

Chris
Chris

Reputation: 8109

You have to flag one property as transient, in order to prevent GORM from trying to save this variable into DB. Try to add this line into your domain class, which contains afterTax.

static transients = ['afterTax']

Upvotes: 0

Related Questions