Doing Grails data-binding interprets empty String values ​​(& ldquo;) as zero (0)

advertisements

This question is about altering how the Grails data-binding handles string-to-integer conversion.

Consider the following domain object:

class Foo {
  String name
  Integer price
}

Furthermore, assume that the domain object is populated from HTTP request parameters:

def foo = new Foo(params).save()

The save() method above will fail if params.price == "" (empty string). I'd like to change this behaviour globally so that an empty string is parsed as zero (0) when converting from a string to an integer in Grails data-binding. How do I achieve that?


added a filter see the setion 5.5.1 Events and Auto Timestamping in the grails documentation (http://grails.org/doc/1.1.x/index.html)

   def beforeInsert = {
       if (price == '') { price = 0}
   }