I'm trying to get grails to validate the contents of a List of objects, might be easier if I show the code first:
class Item {
Contact recipient = new Contact()
List extraRecipients = []
static hasMany = [
extraRecipients:Contact
]
static constraints = {}
static embedded = ['recipient']
}
class Contact {
String name
String email
static constraints = {
name(blank:false)
email(email:true, blank:false)
}
}
Basically what i have is a single required Contact ('recipient'), this works just fine:
def i = new Item()
// will be false
assert !i.validate()
// will contain a error for 'recipient.name' and 'recipient.email'
i.errors
What I'd like also do it validate any of the attached Contact
objects in 'extraRecipients' such that:
def i = new Item()
i.recipient = new Contact(name:'a name',email:'email@example.com')
// should be true as all the contact's are valid
assert i.validate()
i.extraRecipients << new Contact() // empty invalid object
// should now fail validation
assert !i.validate()
Is this possible or do I just have to iterate over the collection in my controller and call validate()
on each object in extraRecipients
?