假设存在如下定义的域。
class Book {
String title
Author author
}
现在,我保存这个域的一个实例。作者域有一些限制。因此,在保存期间,对author的验证现在失败了,而不是不保存整个域,我想取消author(author可以为null)并按原样保存标题字符串。换句话说,我如何取消验证失败的任何数量的字段,并保存其余的属性值?有方便的方法吗?谢谢
这可能是以下情况之一:
-
在
beforeInsert hook
中执行以下操作:def beforeInsert() { this.validate() if(this.hasErrors()){ // get all errors and iterate through it and set the field to null for same } }
-
在保存域时,您可以使用
domain.save(validate:false)
谢谢!!