Grails 2.1 and CustomDateEditor doesn't seem to work

523 Views Asked by At

I'm having trouble using CustomDateEditor in Grails 2.1.0

What I'm trying to achieve:

Date class is now rendered as 08/10/2013 00:00:00 CEST but I want to change the format to 08/10/2013 (dd/MM/yyyy format)

What I've done so far:

resource.groovy

beans = {
    customPropertyEditorRegistrar(myutil.CustomPropertyEditorRegistrar)
}

myutil.CustomPropertyEditorRegistrar

package myutil

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

public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat('dd/MM/yyyy'), true)) //this doesn't work
        registry.registerCustomEditor(java.math.BigDecimal.class, new CurrencyPropertyEditor()) //this work so at least the registerCustomEditors method is invoked
    }
}

As you can see in my code there is a CurrencyPropertyEditor that work just fine so I'm confused here.. what's going on?

I've searched all around and it doesn't seems that my code is wrong but still it's not working.

Thank you in advance for any hint.

1

There are 1 best solutions below

5
On BEST ANSWER

If the scope f this is render the date in your own custom format, isn't it better to use the g:formatDate tag? Something like:

 <g:formatDate format="dd/MM/yyyy" date="${date}"/>

Check formatDate documentation