0

有没有比验证器更好的方法来强制整数精确,例如 2 位数字?

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

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

也许是大整数?

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

4

2 回答 2

7

您可以在字段上设置一个介于 10 到 99 之间的约束:

class FantasyDomainClass {
    Integer twoDigitInteger

    static constraints = {
        twoDigitInteger min:10, max:99
    }
}
于 2012-02-15T15:09:32.670 回答
1

我会使用自定义验证器并将其设置为

class FantasyDomainClass {

Integer twoDigitInteger

static constraints = { 
  twoDigitInteger validator: { 
    return (it.toString().size() <= 2) 
  } 
}
于 2012-02-15T09:28:24.233 回答