5

オブジェクトのリストの内容を検証するために 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)
  }
}    

基本的に私が持っているのは、必要な連絡先(「受信者」)が1つだけです。これはうまく機能します:

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ですか?

4

1 に答える 1