Grails - Domain nullable

Purpose

  • 允许一个 String 类型的字段被设置成 null,默认Grails不能为空。
class Domain {
    Integer id
    String url

    static constraints = {
        url nullable: true
    }
}

Description

  • 当表单的输入框没有值的时候,Web 请求提交的是 blank 字符串,而不是 null
  • 在将多个值绑定的 property 的时候需要注意这点。
  • 使用默认的配置时,系统会将 blank 转换成 null,来对应系统默认设置的 nullable: false
  • 我们可以通过如下地方修改这个默认转换行为
grails-app/conf/application.groovy
// the default value for this property is true
grails.databinding.convertEmptyStringsToNull = false

Reference

https://docs.grails.org/latest/ref/Constraints/nullable.html

posted @ 2020-04-30 10:03  duchaoqun  阅读(217)  评论(0编辑  收藏  举报