Alexx
Alexx

Reputation: 3692

grails: min constraint and attributes of BigDecimal type (type mismatch?)

In the grails class, I have specified

BigDecimal  amount

to represent an amount of money in a transaction.

Then, in the constraints section, I specified:

amount      ( nullable:false, min : 100)

To indicate that the minimum amount accepted would be a dollar. This compiles just fine, but runtime throws a type incompatibility error:

2011-10-26 15:17:06,116 [main] ERROR validation.ConstrainedProperty  - Exception thrown applying constraint [min] to class [class com.mycorp.mypkg.MyProprietaryDefinition] for value [100]: Parameter for constraint [min] of property [amount] of class [class  com.mycorp.mypkg.MyProprietaryDefinition] must be the same type as property: [java.math.BigDecimal]

So am I to safely assume that I cannot specify min values on BigDecimal attributes?

Thank you!

Alexx

Upvotes: 2

Views: 5914

Answers (3)

James Allman
James Allman

Reputation: 41188

Grails expects the min and max values to be the same datatype as the associated property while the scale value is always required to be an Integer. Casting to BigDecimal or adding .0 to the end of the value fixes the problem.

amount(nullable: false, min: 100.0, max: 9999.99, scale: 2)

I recently filed IntelliJ IDEA bug report IDEA-75471 about this very issue. IDEA 10.5.2 incorrectly flags an inspection warning if the min and max values are not integer and the scale value is not the same datatype as the property. The warning can be ignored using //noinspection GroovyAssignabilityCheck.

Upvotes: 7

schmolly159
schmolly159

Reputation: 3881

A groovy cast to the min value should also work.

 min: 0 as BigDecimal

or possibly

 min: 0G

Upvotes: 2

Alexx
Alexx

Reputation: 3692

Interesting. Any of the "min" values I specified on BigDecimal attributes were rejected. I commented them all out, and deployed the app successfully. I tried tacking on ".0" to the end of one min value, and the app rebuilt and redeployed. One by one, I repeated this, save for the last commented min. So then I uncommented it as it was, min : 0, and the app broke.

So I guess that is the answer, that if you don't have a decimal portion to your min (or max, I'd also assume), you need to add one.

Upvotes: 0

Related Questions