[英]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.