views:

626

answers:

1

Hi,

In my Grails app, I need to bind a request parameter to a Date field of a command object. In order to perform the String-to-Date conversion, one needs to register an appropriate PropertyEditor in grails-app\conf\spring\resources.groovy

I've added the following bean definiton:

import org.springframework.beans.propertyeditors.CustomDateEditor
import java.text.SimpleDateFormat

    beans = {
        paramDateEditor(CustomDateEditor, new SimpleDateFormat("dd/MM/yy"), true) {} 
    }

But I'm still getting an error:

java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "04/01/99"]

I think there's probably just something wrong with the way I've defined the bean, but I've no idea what?

+3  A: 

The piece you are missing is registering of the new property editor. The following worked for me when I upgraded to Grails 1.1 and had to bind dates in the MM/dd/yyyy format.

grails-app/config/spring/resources.groovy:

beans = { 
    customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar) 
}

src/groovy/util/CustomPropertyEditorRegistrar.groovy:

package util 

import java.util.Date 
import java.text.SimpleDateFormat 
import org.springframework.beans.propertyeditors.CustomDateEditor 
import org.springframework.beans.PropertyEditorRegistrar 
import org.springframework.beans.PropertyEditorRegistry 

public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { 
  public void registerCustomEditors(PropertyEditorRegistry registry) { 
      registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yy"), true)); 
  } 
}
John Wagenleitner