4
我試圖讓Grails的驗證對象的名單中的內容,如果我告訴代碼首先可能會更容易: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 protected]')
// 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()
這是可能的還是我只需要遍歷我的控制器中的集合,並在extraRecipients
中的每個對象上調用validate()
?
謝謝我在今晚晚些時候會有一個bash – 2009-06-25 11:37:18