Search code examples
validationgrailsintegergrails-ormdigits

Grails: specify number of digits for integer in domain class


Is there some better way than validators to enforce an integer be exactly, for example, 2 digits?

In my fantasy world I would do something like this:

class FantasyDomainClass{
  Integer[2] twoDigitInteger  //fantasy world knows I mean base 10
}

Maybe BigInteger?

Based on the answers posed, I am considering I may not want an integer as '01' is an acceptable value.


Solution

  • I would go with a custom validator and set it as

    class FantasyDomainClass {
    
    Integer twoDigitInteger
    
    static constraints = { 
      twoDigitInteger validator: { 
        return (it.toString().size() <= 2) 
      } 
    }