简体   繁体   中英

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:

     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)

Thanks!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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