{"id":1937,"date":"2010-10-15T00:11:19","date_gmt":"2010-10-14T18:41:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1937"},"modified":"2010-10-15T00:14:28","modified_gmt":"2010-10-14T18:44:28","slug":"gorm-feature-bypass-grails-domain-constraints","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gorm-feature-bypass-grails-domain-constraints\/","title":{"rendered":"GORM feature: Bypass Grails Domain Constraints"},"content":{"rendered":"<p>I didn&#8217;t know this earlier, but found a very good feature of GORM &#8211; &#8216;bypass&#8217; constraints or &#8216;customize&#8217; validation while saving a domain-object. I am not sure if there&#8217;s any name for this feature. But I am naming it as &#8216;bypass constraints&#8217;.<\/p>\n<p>[groovy]<br \/>\nif(domainObject.validate([&#8216;field1&#8217;, &#8216;field2&#8217;])){<br \/>\n  domainObject.save(validate:false, flush:true)<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>Let&#8217;s discuss it with a use case. We create a user table with fields &#8211; id, name, email, password.<br \/>\nAnd email\/password are mandatory fields. <\/p>\n<p>[groovy]<br \/>\nClass User{<br \/>\n    Long id<br \/>\n    String name<br \/>\n    String email<br \/>\n    String password<\/p>\n<p>    static constraints = {<br \/>\n        name(nullable: true)<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>Now there&#8217;s a requirement in the application &#8211; An existing user can <strong>invite a friend<\/strong>.<br \/>\n<\/p>\n<p>With the help of &#8216;bypass constraints&#8217; feature, it&#8217;s easier to handle this use-case without actually creating any separate domain to store invited users information. <br \/>\nFollowing code will create User entry with null password.<br \/>\n[groovy]<br \/>\nUser user = new User(name:userName, email:userEmail)<br \/>\nif(user.validate([&#8217;email&#8217;]) &amp;&amp; user.save(validate:false, flush:true)){<br \/>\nreturn true<br \/>\n}<br \/>\n[\/groovy]<br \/>\n<\/p>\n<p><strong>NOTE<\/strong>: To achieve this, you just need to set &#8216;password&#8217; column as nullable true in your &#8216;user&#8217; table.<br \/>\n<\/p>\n<p>Above example\/use-case is just to explain &#8216;bypass constraints&#8217; feature, nothing more than this. \ud83d\ude42<br \/>\n<\/p>\n<p>I hope it might help you somewhere.<br \/>\n<\/p>\n<p>Salil Kalia<br \/>\nsalil [at] intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t know this earlier, but found a very good feature of GORM &#8211; &#8216;bypass&#8217; constraints or &#8216;customize&#8217; validation while saving a domain-object. I am not sure if there&#8217;s any name for this feature. But I am naming it as &#8216;bypass constraints&#8217;. [groovy] if(domainObject.validate([&#8216;field1&#8217;, &#8216;field2&#8217;])){ domainObject.save(validate:false, flush:true) } [\/groovy] Let&#8217;s discuss it with a use [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1937"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1937\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}