views:

893

answers:

3

The reference doc says that the size constraint: "Uses a Groovy range to restrict the size of a collection or number or the length of a String."

When I put a size constraint on an integer, I get a warning "Property [prop] of domain class TheClass has type [java.lang.Integer] and doesn't support constraint [size]. This constraint will not be checked during validation."

Is the doco wrong?

I know I could use range but it would generally be easier to be able to specify the amount of digits in the number rather than the actual value (like a social security number must have 7 digits or whatever it is, rather than making a range of 1000000 - 9999999).

A: 

If you want the number of digits, make sure it's positive and has a certain length:

myInteger( validator: {
   return it > 0 &&  (it.toString.length) == 7
})
Robert Munteanu
+1  A: 

You can also use max to constrain an integer like myIntProp(max:9999999)

Dave Klein
That would just be like using a range. It would generally be easier to be able to specify the amount of digits in the number rather than the actual value (like a social security number must have 7 digits or whatever it is, rather than making a range of 1000000 - 9999999).
Fletch
It would be easier if it were possible, but I don't see how a custom validator is easier than either a range or max. Another thing to consider is that if you are working with "numbers" like SSN or US zip codes that can contain leading zeros, it might be better to use a String anyway. Just a thought. -- Dave
Dave Klein
Well there's room for error when you type 7 0's in a row, that's all. Nevertheless I will probably settle for this rather than the custom validator I think. The String point is valid... but I think it's all around easier to use a number, and just format it to display a certain amount of digits (i.e. include leading zeros).
Fletch
A: 

I found the answer whilst searching JIRA: http://jira.codehaus.org/browse/GRAILS-947. The doco is wrong. Looks like it's up to the custom validator.

Fletch