grailsgroovyconstraintsgrails-domain-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.


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
    }
}

Solution

  • By default, all domain class properties are not nullable (i.e. they have an implicit nullable: false constraint).

    Constraints:

    If the string is null, it won’t validate with blank: true. In this case, set the nullable constraint to true.

    But why not just leave blank as constraint, i think both nullable and blank will have same function

    Web requests resulting from form submissions will have blank strings, not null, for input fields that have no value. 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. This includes empty strings and blank strings.

    A blank string is any string such that the trim() method returns an empty string. To turn off the conversion of empty strings to null set the grails.databinding.convertEmptyStringsToNull property to false in application.groovy.

    Please refer documentation for more details.

    Hope this will helps you.