0

I checked out Grails: How to combine domain objects' errors with command objects' errors?, but for some reason the solutions aren't working for me. It's possible they're only for Grails 1.3.7 and not Grails 2.2.1.

I have a command object OuterObjectCommand that contains a list of InnerObjectCommand. I'm populating it manually in the controller, and then calling validate. Calling validate() on OuterObjectCommand does not appear to be validating the InnerObjectCommand list, so I validate all the elements of the list separately. What I want to do is add all the errors of InnerObjectCommand objects and the OuterObjectCommand object into flash.errors. How can I do this?

Community
  • 1
  • 1
Anonymous1
  • 3,877
  • 3
  • 28
  • 42
  • Have you tried using custom [validator](http://grails.org/doc/latest/ref/Constraints/validator.html) in the outer command object which would explicitly validate the inner command objects? – dmahapatro Jul 03 '13 at 17:08
  • I'm not sure how to write that. static constraints = { innerObjectCommand validator: { it.validate(); ??? } – Anonymous1 Jul 03 '13 at 18:11

1 Answers1

0

Have a look at the last example in the validator page.

Vaguely, you would need something like below:

class ParentCommand {
    List<ChildCommand> childCommands
    static constraints = {
        childCommands validator: {val, obj ->
            def errors = []
            val.each{
                 errors << (!it.validate() ? it.errors.allErrors : [])
            }

            errors?.flatten()
        }
    }
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117