waterbottle27
waterbottle27

Reputation: 303

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!

Upvotes: 0

Views: 108

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

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.

Upvotes: 1

Related Questions