I am new to Grails/GSP and want to implement date validation in Grails. Validation should be for input text in text field as required format is mm/dd/yyyy. Any guidance is appreciated. Also please point me to some good Grails and GSP tutorials if any one knows. Thanks in advance.
Asked
Active
Viewed 1,846 times
2 Answers
0
To require a specific date format for date input, you can use a custom property editor.
In src/groovy/
you can add a class to register the custom editor which will be used in data binding.
class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
void registerCustomEditors(PropertyEditorRegistry registry) {
def format = 'MM/dd/yyyy'
def dateFormat = new SimpleDateFormat(format)
dateFormat.setLenient(false)
registry.registerCustomEditor(Date, new CustomDateEditor(dateFormat, true, format.size()))
}
}
and register the bean in resources.groovy
beans = {
customPropertyEditorRegistrar(CustomPropertyEditorRegistrar)
}

doelleri
- 19,232
- 5
- 61
- 65
-
It is possible to validate based on multiple date formats? Let's say I want to allow `MM-dd-yyyy` and `yyyy-MM-dd`, can I do that? – raffian Feb 24 '13 at 00:44
0
Grails has a default date and time picker that it uses in scaffolds, you can use that but if you want a textbox to enter the date, you can validate the format using jQuery or javascript, then parse the string into date using:
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
date = format.parse(stringDate);
}catch (Exception e) {
//something
}
You can use regular expression with javascript to validate the format of entered date in view. This might be useful for that.

Community
- 1
- 1

Eddard Stark
- 3,575
- 8
- 35
- 51