Emmesk Dosk
Emmesk Dosk

Reputation: 93

Grails domain class syntax explanation

class User {   
    static constraints = {  
       password(unique:true, length:5..15, validator:{val, obj -> 
          if(val?.equalsIgnoreCase(obj.firstName)) { 
              return false  
          }  
       } 
    } 
}

I'm finding this groovy syntax really confusing. I've spent a few days trying to learn grails/groovy. I know what it does but I don't really understand it.

Can someone explain how this works?

What is constraints? Is it a closure, with password being called as a function? How does it get called?

What about the validator what kind of syntax is that?

Like I said I can see what it does, I just don't understand how it does it.

Upvotes: 1

Views: 286

Answers (2)

wangf
wangf

Reputation: 925

I have same question and also cannot find the syntax explained in Groovy book/document. Then I google and find this blog: http://www.artima.com/weblogs/viewpost.jsp?thread=291467, which answered my question.

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50275

Preface - Basic knowledge of Groovy usage is required in some parts.

//Domain Class
class User {
    //A property of the domain class 
    //corresponding to a column in the table User.
    String password

    //Domain classes and Command objects are treated specially in Grails
    //where they have the flexibility of validating the properties 
    //based on the constraints levied on them.
    //Similar functionality can be achieved by a POGO class
    //if it uses @Validateable transformation at the class level.   
    static constraints = {  
       //Constraints is a closure where validation of domain class
       //properties are set which can be validated against when the domain class
       //is saved or validate() is called explicitly.
       //This is driven by AbstractConstraint and Constraint java class 
       //from grails-core

       //Constraint on password field is set below.
       //Here password is not treated as a method call
       //but used to set the parameter name in the constraint class on which
       //validation will be applied against. Refer AbstractConstraint 
       //for the same.
       password(unique:true, size:5..15, validator:{val, obj -> 
          //Each of the key in the above map represents a constraint class 
          //by itself
          //For example: size > SizeConstraint; validator > ValidatorConstraint
          if(val?.equalsIgnoreCase(obj.firstName)) { 
              return false  
          }  
       } 
    } 
}

Above mentioned are the basics of how constraints work. If you need to know more in details then here are some of the sources you definitely need to visit if you get any further question about its usage:

Obviously, SO is good place to ask questions if you are stuck in any implementation related to constraint or more. Feel free to use this community to help you out in surmounting problematic programmatic situations.

Upvotes: 2

Related Questions