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
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.
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.