我正在尝试验证SELECT。通常我会使用inList,因为SELECT意味着固定数量的字符串,但我想知道是否有更优雅的东西。在这种情况下,我有一个带有SELECT输入的表单,其值为0-24,对应于接下来的24个月。
在我的cmdObject中我有
class FormCommand {
String startSlot
static constraints = {
// startSlot(nullable:false, blank:false, range:0..24)
startSlot(nullable:false, blank:false,
validator: { val, obj -> val.toInteger() < 25})
}
}
我希望能够使用范围:0..24语句,但根据我对范围的理解,它们不适用于表单生成的字符串。
是否有一种首选方法将传入的字符串转换/绑定到int中,因此我可以使用范围:0..24?还是有另一种方法来处理这个问题?
我能做到
inList: [ "0", "1", /* type them all out */, "24" ]
或者写一些更强大的自定义验证器,但我想知道是否有更多的groovy / grails解决方案。
感谢。
答案 0 :(得分:2)
您可以使用带有字符串的范围,如此...
class FormCommand {
String startSlot
static constraints = {
startSlot(nullable: false, size: '0'..'24')
}
}
答案 1 :(得分:1)
class FormCommand {
Integer startSlot
static constraints = {
startSlot(nullable: false, size: 0..24)
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
最终答案结果是这里提出了一些建议:
class FormCommand {
Integer startSlot
static constraints = {
startSlot(nullable: false, range: 0 .. 24)
}
}
(http://grails.org/doc/latest/ref/Constraints/range.html)的文档告诉我它可以用于下一个/上一个的任何东西。我不知道你可以通过将字段指定为Integer来隐式地将param.startSlot转换为Integer。
感谢。