如何确保在 Grails 中设置布尔字段?

发布于 2024-10-03 08:28:36 字数 252 浏览 8 评论 0原文

我想确保检查代表布尔值的两个表单字段之一。但这样做没有适当的限制。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿越时光隧道 2024-10-10 08:28:36

您还可以在控制器中检查这一点,例如

if (params.checkBox1 != 'on' && params.checkBox2 != 'on')
  flash.error = 'At least one value must be checked.'
  return ...

You can also check this in the Controller, e.g.

if (params.checkBox1 != 'on' && params.checkBox2 != 'on')
  flash.error = 'At least one value must be checked.'
  return ...
浅沫记忆 2024-10-10 08:28:36

也许最简单的方法是使用确保选择值的表单。因此,创建单选按钮而不是复选框是更好的解决方案。它也将直接代表您的意图。

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.

猛虎独行 2024-10-10 08:28:36

您可以编写自己的自定义验证器。

像编辑这样的东西

selfInspecting(validator: {val, obj -> /*test selfInspecting here*/})

——响应另一个答案——你可以在表单上处理这个问题,但你也应该在服务器上处理它。

另一个编辑——评论中建议您可能想要验证 Domain 类中的两个字段之一。使用自定义验证器也可以轻松完成此操作。对于自定义验证器闭包的上述签名,val 是值 selfInspecting,obj 是域对象实例。所以你可以有

{ val, obj ->

    if (val == null) return false // if you want to ensure selfInspecting is not null
    else return true

    ... or ...

    // if you want to check that at least 1 of two fields is not null
    def oneOrTheOther = false
    if (obj.field1 != null || obj.field2 != null) 
       oneOrTheOther = true
    return oneOrTheOther

}

you can write your own custom validator.

something like

selfInspecting(validator: {val, obj -> /*test selfInspecting here*/})

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

{ val, obj ->

    if (val == null) return false // if you want to ensure selfInspecting is not null
    else return true

    ... or ...

    // if you want to check that at least 1 of two fields is not null
    def oneOrTheOther = false
    if (obj.field1 != null || obj.field2 != null) 
       oneOrTheOther = true
    return oneOrTheOther

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文