2012-02-15 32 views
0

是否有比验证器更好的方式来强制执行一个整数,例如2位数?Grails:指定域类中整数的位数

在我的幻想世界,我会做这样的事情:

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

也许BigInteger的?

根据提出的答案,我正在考虑我可能不想要一个整数,因为'01'是一个可接受的值。

回答

2

我会去与a custom validator并将其设置为

class FantasyDomainClass { 

Integer twoDigitInteger 

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

您可以在现场设置了contraint,这是10 99之间:

class FantasyDomainClass { 
    Integer twoDigitInteger 

    static constraints = { 
     twoDigitInteger min:10, max:99 
    } 
}