![](/img/trans.png)
[英]How to reuse custom validation logic in multiple fields from the same Domain
[英]How to nullify the fields that fail validation and save the domain?
假设有一个定义如下的域。
class Book {
String title
Author author
}
现在,我保存了这个域的一个实例。 作者域有一些限制。 因此,在保存作者验证期间现在失败而不是不保存整个域,我想取消作者(作者可以为空)并按原样保存标题字符串。 换句话说,我如何取消验证失败的任意数量的字段并保存其余的属性值? 有没有方便的方法呢? 谢谢!
这可能是以下之一:
在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(验证:假)
谢谢!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.