简体   繁体   中英

nullable: false in Grails Domain Class constraints

I am having code

static constraints = {
    stringValiable1(nullable: false)
    stringValiable2(nullable: false)
    stringValiable3(nullable: false)
}

Is there a method where I can abstract out the (nullable: false) so that I avoid using it again and again. Also can it be done at package level so that I can use it throughout the other classes as well.

What you're trying to factor out is simply a Map . For example, the method call stringValiable1(nullable: false) is syntatic sugar for stringValiable1([nullable: false]) .

So, factoring it out is a matter of putting a Map in some class, and then using it:

Place to store Map

package foo.bar

class MyConstraints {
    static nullable = [nullable: true]
    static blank = [blank: true]
}

The domain class

static import foo.bar.MyConstraints.*

class SomeDomainClass {
    ...
    static constraints = {
        stringVariable1(nullable)
        stringVariable2(nullable + blank)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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