如何确保在 Grails 中设置布尔字段?
我想确保检查代表布尔值的两个表单字段之一。但这样做没有适当的限制。 nullable: false
不起作用。
class Organisation {
Boolean selfInspecting
static constraints = {
selfInspecting(nullable: false)
}
}
如何检查两个字段之一是否被选中?
I want to ensure that one of two form fields representing a boolean value is checked. But there is no appropriate constraint to do this. nullable: false
does not work.
class Organisation {
Boolean selfInspecting
static constraints = {
selfInspecting(nullable: false)
}
}
How can I check whether one of the two fields is checked or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以在控制器中检查这一点,例如
You can also check this in the Controller, e.g.
也许最简单的方法是使用确保选择值的表单。因此,创建单选按钮而不是复选框是更好的解决方案。它也将直接代表您的意图。
Perhaps the simplest approach is to use a form that ensures a value is picked. As such, creating a radio buttons rather than checkboxes is a better solution. It would directly represent your intent as well.
您可以编写自己的自定义验证器。
像编辑这样的东西
——响应另一个答案——你可以在表单上处理这个问题,但你也应该在服务器上处理它。
另一个编辑——评论中建议您可能想要验证 Domain 类中的两个字段之一。使用自定义验证器也可以轻松完成此操作。对于自定义验证器闭包的上述签名,val 是值 selfInspecting,obj 是域对象实例。所以你可以有
you can write your own custom validator.
something like
EDIT -- in response to the other answer -- you can handle this on the form, but you should also handle it on the server.
ANOTHER EDIT -- It was suggested in a comment that you might want to validate one of two fields on your Domain class. This is also easily accomplished with a custom validator. With the signature above for the custom validator closure, the val is the value selfInspecting, and obj is the domain object instance. So you could have