Search code examples
grailsgrails-orm

Grails domain hasMany validation


I have a case that validation is done on domain properties but not on the an associated (hasMany) properties.

Is there any configuration I can add to enable the validation on both properties (domain and hasMany).

grails version : 3.1.14

Example:

class Person {
      String name;
      static hasMany = [location: Location]
      static constraints = {
        name nullable: true
      }
}

class Location {
      String address
      String city
      State state
      String zip

      static constraints = {
        address nullable: true
      }
}

Solution

  • According to the documentation the validation should work for has-many associations as you wish: http://docs.grails.org/3.1.14/ref/Domain%20Classes/validate.html

    But in my test's it does not work eather.

    An other solution is to work with the constraints:

    static constraints = {
        name nullable: true
        location validator: {val, obj ->
            val.every { it.validate() } ?: 'invalid' 
        }
    }