Uploaded image for project: 'Grails'
  1. Grails
  2. GRAILS-9895 Improve Data Binding
  3. GRAILS-6901

Make StringTrimmerEditor the default property editor for String

    Details

    • Type: Sub-task Sub-task
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.3.5
    • Fix Version/s: 2.3-M1
    • Component/s: Controllers
    • Labels:
      None

      Description

      Is there a reason why org.springframework.beans.propertyeditors.StringTrimmerEditor is not the default property editor for Strings in Grails?
      I can't find any use-case in my apps where I want to preserve white-space before or after user input, or store empty strings in the db.

      To avoid query problems I always have to register a custom property editor for String in my applications. (one registrar class and one entry in resources.groovy)

      I would suggest a change of default editor for String, or at least make it easier to change the default.

      Feel free to push this issues in a suitable direction (use StringTrimmerEditor, and/or make it easier to plugin custom property editors)

        Activity

        Hide
        Goran Ehrsson added a comment -

        For those interested, this is how I change the property editor for String today (1.3.5)

        package my.company;
        
        import org.springframework.beans.PropertyEditorRegistrar;
        import org.springframework.beans.PropertyEditorRegistry;
        import org.springframework.beans.propertyeditors.StringTrimmerEditor;
        
        public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
        
            public void registerCustomEditors(PropertyEditorRegistry registry) {
                // We want all strings to be trimmed and empty strings converted to null.
                registry.registerCustomEditor(String.class, new StringTrimmerEditor(true));
            }
        }
        

        Then in resources.groovy:

        customPropertyEditorRegistrar(my.company.CustomPropertyEditorRegistrar)
        
        Show
        Goran Ehrsson added a comment - For those interested, this is how I change the property editor for String today (1.3.5) package my.company; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.propertyeditors.StringTrimmerEditor; public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { // We want all strings to be trimmed and empty strings converted to null. registry.registerCustomEditor(String.class, new StringTrimmerEditor(true)); } } Then in resources.groovy: customPropertyEditorRegistrar(my.company.CustomPropertyEditorRegistrar)
        Hide
        Joshua Burnett added a comment -

        currently the default in GrailsDataBinder is
        binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
        So there currently exists a PropertyEditorSupport class for String, just be aware that this could be a "breaking" change

        I think the value StringTrimmerEditor is much more common and this has my vote.

        Show
        Joshua Burnett added a comment - currently the default in GrailsDataBinder is binder.registerCustomEditor(String.class, new StringMultipartFileEditor()); So there currently exists a PropertyEditorSupport class for String, just be aware that this could be a "breaking" change I think the value StringTrimmerEditor is much more common and this has my vote.
        Hide
        Konstantinos Kostarellis added a comment -

        I created a plugin for the meantime offering the proposed fix

        http://grails.org/plugin/trimmer

        Show
        Konstantinos Kostarellis added a comment - I created a plugin for the meantime offering the proposed fix http://grails.org/plugin/trimmer
        Hide
        Graeme Rocher added a comment -

        Jeff - this should be handled as part of the data binding rewrite efforts.

        Show
        Graeme Rocher added a comment - Jeff - this should be handled as part of the data binding rewrite efforts.
        Hide
        Jeff Scott Brown added a comment -

        In 2.3 Strings are trimmed by default during data binding. The way to turn this behavior off is to set the grails.databinding.trimStrings property to false in Config.groovy

        grails-app/conf/Config.groovy
        // the default value is true
        grails.databinding.trimStrings = false
        
        // ...
        
        
        Show
        Jeff Scott Brown added a comment - In 2.3 Strings are trimmed by default during data binding. The way to turn this behavior off is to set the grails.databinding.trimStrings property to false in Config.groovy grails-app/conf/Config.groovy // the default value is true grails.databinding.trimStrings = false // ...

          People

          • Assignee:
            Jeff Scott Brown
            Reporter:
            Goran Ehrsson
          • Votes:
            11 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Development