0
votes

I am fetching list of results from database but the list doesn't validates the constraints until i call "validate()" method for each objects.

    def temp = ConfigInfo.where { projectId == project }.findAll()

    //at this stage domain class is not validated
    temp.each {         
        println "validate" + it.validate()
        println "has error" +  it.hasErrors()
    } 
          //in the above code block validate() is called, I don't want to do this. 

// is it possible to configure it happen automatically and i get the validated objects.

I don't want to invoke validate method. is there any way or configuration in grails such that domain class get validated automatically.

Please help me in this.

2
If you are getting your data from the table and not changing values why do you need to validate it again? - Alidad

2 Answers

0
votes

Grails domain class objects get validated before save.

Use the following in your domain model to set the validation rules:

static constraints = {
   // whatever you want to validate
}

Whenever you save an object it will be validated by the constraints you set. When the object is persistent in the database it has always been validated.

0
votes

All you need to do is that just define all your validation in constraints closure. e.g.

    static constraints = {
                id maxLength: 5
                //etc...
}

at the time of saving object you just need to check object is validate or not!, it will return true/false. look this: click here