Here is a small recipe for those who are not using the DatePicker default Grails.
The Grails comes with a tag that is g:datepicker. It generates listboxes (comboboxes) for day, month, year, hour, minute and second. You can even choose which of these comboboxes you want to generate.
But there are times we do not want to use these combos, and just want a text field where the user can type the date in any format, such as dd/MM/yyyy or dd/MM/yy, or yyyyMMdd, or .. . ... or ... or
Also, if you have an internationalized application, there may users with Portuguese Locale and input format like dd/MM/yyyy, or others with an English Locale and MM/dd/yyyy format.
Well, the goal here is to show how to configure Grails to use different input formats for dates, so it can parse automatically whenever you have an attribute of type Date in a domain object (or a Command object) that we need to bind from the parameters received in a POST (or GET).
Here it goes:
1) User fills out a field input type = "text", with a date in a format that you determine, ex: MM/dd/yyyy
2) He/she sends this POST, which gets to your Controller and you have the params attribute
3) you want to automaticaly bind (let's say my domain object is Person, and in it there is a Date birthday):
Person p = new Person(params)
package com.myapp.editor;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.i18n.LocaleContextHolder;
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
def messageSource;
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(messageSource.getMessage("dateFormat4y",null,'dd/MM/yyyy',LocaleContextHolder.locale )),true));
}
}
Note that to work properly, you need to edit your grails-app/i18n/messages.properties (messages_en_US.properties, messages_en.properties) to have each one its correct setting. Ex for the en:
dateFormat4y=MM/dd/yyyy
beans = {
customPropertyEditorRegistrar(com.myapp.editor.CustomPropertyEditorRegistrar) {
messageSource = ref('messageSource')
}
}
That's it!
Cheers
Thanks, this is too useful to me
ReplyDeleteExcellent!
ReplyDeleteClearly explained and efficient!
Thanks a lot
Great! This save my life!
ReplyDelete