我正在尝试让 grails 验证对象列表的内容,如果我先显示代码可能会更容易:
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)
}
}
基本上我所拥有的是一个必需的联系人('收件人'),这很好用:
def i = new Item()
// will be false
assert !i.validate()
// will contain a error for 'recipient.name' and 'recipient.email'
i.errors
我还想要验证Contact
“extraRecipients”中的任何附加对象,例如:
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()
这是可能的还是我只需要遍历我的控制器中的集合并调用validate()
每个对象extraRecipients
?