{"id":4903,"date":"2011-12-29T23:17:16","date_gmt":"2011-12-29T17:47:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4903"},"modified":"2011-12-29T23:19:11","modified_gmt":"2011-12-29T17:49:11","slug":"grails-custom-data-binding-in-3-simple-steps","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-custom-data-binding-in-3-simple-steps\/","title":{"rendered":"Grails Custom Data Binding in 3 Simple Steps"},"content":{"rendered":"<p>The other day, <a href=\"http:\/\/www.tothenew.com\/blog\/author\/farid\/\" target=\"_blank\">Farid<\/a> came with an interesting problem of binding a Time String to java.sql.Time. We straight away found the answer in using a CustomTimeEditor and registering it using a CustomPropertyEditorRegistrar bean. We were able to come arrive at this solution, thanks to this<a href=\"http:\/\/stackoverflow.com\/questions\/2871977\/binding-a-grails-date-from-params-in-a-controller\" target=\"_blank\"> StackOverflow thread<\/a>.<\/p>\n<p>This set me thinking into using a CustomProperty Editor for, say a Cost object, which could contain a unit and an amount<\/p>\n<p>[java]<\/p>\n<p>class Cost{<\/p>\n<p>String unit \/\/Could be constrained to belong to some range of values<\/p>\n<p>BigDecimal amount<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>I was able to solve this issue in 3 simple steps which I would like to share with you. I felt that this would take a lesser amount of time compared to the Extended Data Binding Plugin(my gut feeling)<\/p>\n<p><strong>1. Add a CustomCostEditor to src\/groovy<\/strong><\/p>\n<p>[java]<\/p>\n<p>import java.beans.PropertyEditorSupportimport org.springframework.util.StringUtils<br \/>\nclass CustomCostEditor extends PropertyEditorSupport {\u00a0 \u00a0 private final boolean allowEmpty<br \/>\n        CustomCostEditor(boolean allowEmpty) {<br \/>\n               this.allowEmpty = allowEmpty<br \/>\n        }<br \/>\n        @Override<br \/>\n        void setAsText(String text) {<br \/>\n               if (this.allowEmpty &amp;&amp; !StringUtils.hasText(text)) {<br \/>\n                     \/\/ Treat empty String as null value.<br \/>\n                     setValue(null);<br \/>\n               } else {<br \/>\n                      setValue(parse(text))<br \/>\n               }<br \/>\n        }<br \/>\n        @Override<br \/>\n        String getAsText() {<br \/>\n                Cost cost = (Cost) getValue()<br \/>\n                return &quot;${cost.unit} ${cost.amount}&quot;<br \/>\n        }<br \/>\n       Cost parse(String text){<br \/>\n                try{<br \/>\n                     new Cost(unit: text.substring(0, text.indexOf(&quot; &quot;)), amount: text.substring(text.indexOf(&quot; &quot;) + 1).toBigDecimal())<br \/>\n                } catch(Exception exception){<br \/>\n                     throw new IllegalArgumentException(&quot;Cost should be of the format &#8216;Unit Amount&#8217;&quot;)<br \/>\n                }<br \/>\n       }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>The methods which we need to override are setAsText() and getAsText()<br \/>\n<strong> 2. Add a CustomPropertyEditorRegistrar class, which has a registerCustomEditorMethods<\/strong><\/p>\n<p>[java]<br \/>\nimport org.springframework.beans.PropertyEditorRegistrar<br \/>\nimport org.springframework.beans.PropertyEditorRegistry<\/p>\n<p>class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar{<br \/>\n    public void registerCustomEditors(PropertyEditorRegistry registry) {<br \/>\n        registry.registerCustomEditor(Cost.class, new com.intelligrape.example.CustomCostEditor(true));<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>3. Add a spring bean to add an instance of this custom property editor registrar<\/strong><br \/>\nFinally, we create a spring bean by adding the following line in resources.groovy<\/p>\n<p>[java]<br \/>\nbeans = {<br \/>\n    customPropertyEditorRegistrar(CustomPropertyEditorRegistrar)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now, assuming that we have an embedded field cost of type Cost in a domain class Item, we can simply use<\/p>\n<p>[html]<br \/>\n&lt;input type=&quot;text&quot; name=&quot;cost&quot; value=&quot;${fieldValue(bean: item, field:&#8217;cost&#8217;)}&quot;\/&gt;<br \/>\n[\/html]<\/p>\n<p>and input <strong>&#8220;$ 123&#8221;<\/strong> to store <strong>$<\/strong> in the <strong>cost.unit<\/strong> and <strong>123<\/strong> in the <strong>cost.amount<\/strong>.<\/p>\n<p>I believe that the possibilities with such an approach are infinite!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day, Farid came with an interesting problem of binding a Time String to java.sql.Time. We straight away found the answer in using a CustomTimeEditor and registering it using a CustomPropertyEditorRegistrar bean. We were able to come arrive at this solution, thanks to this StackOverflow thread. This set me thinking into using a CustomProperty [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":18},"categories":[7],"tags":[733,735,734,664],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4903"}],"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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4903"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4903\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}