简体   繁体   中英

Grails/Groovy: Understanding the parameters of the closure code

I'm new to Grails/Groovy, and so please bear with me as I try to understand a piece of code that I had come across in a book.

It is a simple Album domain class:

class Album {
    String artist
    String title
    List songs = []
    List durations = []


    static constraints = {
        artist blank: false
        title blank: false
        songs minSize:1, validator:{ val, obj ->
            if(val.size() != obj.durations.size())
                return "songs.durations.not.equal.size"
    }
}  

My question comes from the constraints property block of code.
In the validator constraint, the author uses a closure.
But what exactly are "val" and "obj"? What values will they be given?

Also, a bonus question, what type is "constraints"? I don't think it is a map as they are defined as [ ] in Groovy. Coming from a Java perspective, x = { .. } is an array, but I'm not sure it is the same in Groovy.

Thanks for the help!

Your first question, val and obj parameters refer to the value of the property and a pointer to the instance, respectively. The documentation for custom validation routines explains this in further detail.

As for the bonus question constraints is a Groovy closure .

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