Grails验证列表对象 [英] Grails validation of a list objects

查看:150
本文介绍了Grails验证列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$>我试图让Grails验证一个List对象的内容,可能会更容易一些。 c $ c> class Item {
Contact recipient = new Contact()
List extraRecipients = []

static hasMany = [
extraRecipients:Contact
]

静态约束= {}

static嵌入= ['收件人']
}

类联系人{
字符串名称
字符串电子邮件

静态约束= {
名称(空白:false)
电子邮件(电子邮件:true,空白:false)
}

$ / code>

基本上我所拥有的是一个必需的联系人('收件人'),很好:

  def i = new Item()
//将会是false
assert!i .validate()
//将包含'recipient.name'和'recipient.email'的错误
i.errors

我还想让它验证任何公文d 在'extraRecipients'中联系对象,例如:

  def i = new Item()
i.recipient = new Contact(name:'a name',email:'email@example.com')

//应为true,因为所有联系人都是有效的
声明i.validate()

i.extraRecipients<< new Contact()//清空无效对象

//现在应该失败验证
assert!i.validate()

这是可能的,还是我只需迭代我的控制器中的集合,并在每个对象上调用 validate() extrarecipients

解决方案

您希望错误出现在Item域对象上(作为extraRecipients属性的错误,而不是让级联保存对extraRecipients中的单个联系人项引发验证错误,对吗?



如果是这样,您可以使用自定义验证器在你的Item约束中,像这样的东西(这没有经过测试,但应该是关闭的):

  static constraints = { 
extraRecipients(验证者:{收件人 - >
recipients.ev ery {it.validate()}
})
}

您可以比错误信息更奇怪,可能表示错误字符串中的收件人失败,但这是执行此操作的基本方法。


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?

解决方案

If I'm understanding the question correctly, you want the error to appear on the Item domain object (as an error for the extraRecipients property, instead of letting the cascading save throw a validation error on the individual Contact items in extraRecipients, right?

If so, you can use a custom validator in your Item constraints. Something like this (this hasn't been tested but should be close):

static constraints = {
    extraRecipients( validator: { recipients ->
        recipients.every { it.validate() } 
    } )
}

You can get fancier than that with the error message to potentially denote in the resulting error string which recipient failed, but that's the basic way to do it.

这篇关于Grails验证列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆