1

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
      }
}
Hanan Atallah
  • 120
  • 1
  • 8

1 Answers1

1

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' 
    }
}
  • Same suggestion as descripted here: https://stackoverflow.com/questions/16181599/grails-validate-nested-command-object-not-working – gregorr Jun 13 '17 at 08:57
  • Thanks much, I tried something close to your suggestion to call "hasMany" validator in the Persistence Listener level. – Hanan Atallah Jun 17 '17 at 02:16