简体   繁体   English

如何取消验证失败的字段并保存域?

[英]How to nullify the fields that fail validation and save the domain?

Suppose there is a domain as defined below.假设有一个定义如下的域。

class Book {
    String title        
    Author author
}

Now, i save an instance of this domain.现在,我保存了这个域的一个实例。 The author domain has some constraints.作者域有一些限制。 So, during save the validation for author fails now instead of not saving the whole domain i want to nullify the author(The author can be null) and save the title string as it was.因此,在保存作者验证期间现在失败而不是不保存整个域,我想取消作者(作者可以为空)并按原样保存标题字符串。 In other words how do i nullify any number of fields whose validation failed and save rest of the properties values?换句话说,我如何取消验证失败的任意数量的字段并保存其余的属性值? Is there a convenient way to do so?有没有方便的方法呢? Thanks!谢谢!

This could be one of following :这可能是以下之一:

  1. In beforeInsert hook do something like below:beforeInsert hook执行如下操作:

     def beforeInsert() { this.validate() if(this.hasErrors()){ // get all errors and iterate through it and set the field to null for same } }
  2. While saving the domain you could use在保存您可以使用的域时

domain.save(validate:false) domain.save(验证:假)

Thanks!!谢谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM