Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain object string field auto trimming

After update from Grails 2.2.3 to Grails 2.3.5 (groovy 2.0.8->2.1.9) I found strange behavior Domain object:

class Element {
  String name
  String title
  static constraints = {
    title nullable: true
  }
}

During creation String field trims automatically and empty string replaced by null

def e = new Element(name:'', title:'  sgg gg ')
assert e.name==null
assert e.title=='sgg gg'

I can't find this super feature in changelog of Grails & groovy. How I can disable it?

like image 264
demon101 Avatar asked Feb 17 '14 19:02

demon101


1 Answers

From: http://grails.1312388.n4.nabble.com/Grails-2-3-Data-Binding-String-Trimming-And-Null-Conversions-td4645255.html

The default behavior in Grails 2.3 is to trim strings during data binding. In addition to that, another default behavior is to convert empty strings (strings with nothing in them, not even spaces) to null during data binding. Those 2 things happen in that order so if you bind a String with nothing in it but spaces, the default behavior is to bind null because the String will be trimmed and then since it is empty it will be converted to null. This is a sensible default. There are separate config properties for disabling either of those behaviors.

   // grails-app/conf/Config.groovy
   grails.databinding.convertEmptyStringsToNull=false
   grails.databinding.trimStrings=false

I believe it's mentioned here in the documentation

like image 99
tim_yates Avatar answered Oct 26 '22 15:10

tim_yates