简体   繁体   English

grails 域 class 约束中的空白和可为空有什么区别?

[英]What the difference between the blank and nullable in the grails domain class constraints?

Just saw this example for grails constraint, But why not just leave blank as constraint, i think both nullable and blank will have same function.刚刚看到 grails 约束的这个例子,但是为什么不把空白作为约束,我认为可为nullableblank都将具有相同的 function。


class User {
    String firstName
    String lastName
    String passwordHash

    static constraints = {
        firstName blank: false, nullable: false
        lastName blank: false, nullable: false
        passwordHash blank: false, nullable: false
    }
}

By default, all domain class properties are not nullable (ie they have an implicit nullable: false constraint).默认情况下,所有域 class 属性都不可为空(即它们具有隐式nullable: false约束)。

Constraints:约束:

  • blank - Validates that a String value is not blank. blank - 验证字符串值不是空白。 Set to false if a string value cannot be blank.如果字符串值不能为空,则设置为false

If the string is null , it won't validate with blank: true .如果字符串是null ,则不会使用blank: true进行验证。 In this case, set the nullable constraint to true .在这种情况下,将nullable约束设置为true

  • nullable - Allows a property to be set to null . nullable - 允许将属性设置为null By default Grails does not allow null values for properties.默认情况下 Grails 不允许属性的null值。 - defaults to false . -默认false Set to true if the property allows null values.如果该属性允许 null 值,则设置为 true。

But why not just leave blank as constraint, i think both nullable and blank will have same function但是为什么不把空白作为约束,我认为可为空和空白都将具有相同的 function

Web requests resulting from form submissions will have blank strings, not null , for input fields that have no value.对于没有值的输入字段,由表单提交产生的 Web 请求将具有空白字符串,而不是 null Keep this in mind when doing mass property binding to properties that are not nullable.在对不可为空的属性进行批量属性绑定时,请记住这一点。 The default behavior is such that a blank string will not validate for nullable: false since the data binder will convert blank strings to null .默认行为是空白字符串不会验证nullable: false因为数据绑定器会将blank字符串转换为null This includes empty strings and blank strings.这包括空字符串和空字符串。

A blank string is any string such that the trim() method returns an empty string.空白字符串是任何字符串,因此trim()方法返回一个空字符串。 To turn off the conversion of empty strings to null set the grails.databinding.convertEmptyStringsToNull property to false in application.groovy.要关闭空字符串到null的转换,请将 application.groovy 中的grails.databinding.convertEmptyStringsToNull属性设置为false

Please refer documentation for more details.请参阅文档以获取更多详细信息。

Hope this will helps you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM