PDA

View Full Version : Empty date form fields


ssljivic
Jul 15th, 2007, 09:59 AM
I have a problem creating the form with field bound to java.util.Date.
When the form is submitted with blank date filed, the following error is shown:

Property 'dateTo' threw exception; nested exception is java.lang.IllegalArgumentException: The date must not be null

I am using the CustomDateEditor. I have the following initBinder implemented:


protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
super.initBinder(request, binder);

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}

CustomDateEditor is configured to accept the empty values.

I am using the Spring’s form tags.

Can anyone help with this?

Thanks

dr_pompeii
Jul 15th, 2007, 02:29 PM
can you show your bean definition?

maybe the cause is your validator related with the message.properties
, i am not sure, but i think that validations is excuted first and then your binder

regards

ssljivic
Jul 16th, 2007, 03:04 AM
I do not have any validators defined with this controller.

ivankoi
Jul 17th, 2007, 06:37 AM
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
super.initBinder(request, binder);

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}

Well this looks fine to me. Try to leave out super.initBinder(request, binder);
spring default implementation is empty so you might not need it. Maybe this will make gremlins go away.

dr_pompeii
Jul 17th, 2007, 01:22 PM
if you can wait me until tomorrow, i can share my implementation

i can remember 2 tricks about of this, one with validator and the other in the code inplementation

try looking in other threads

regards

pmularien
Jul 17th, 2007, 02:38 PM
Post the whole stack trace (from your server logs, if necessary) - are you sure it's coming from form binding? That exact error message is used in other libraries, most notably Apache Commons DateUtils, which can be used for date comparison and validation.

The code you posted looks fine as far as actual acceptance of an empty field without error.

Please post the bean definition of your controller so that we can make sure that you don't have any other validators hooked up.

ssljivic
Jul 18th, 2007, 03:33 AM
I rechecked the entire code and pmularien was right.
The issue was not in the binding part, but rather in part of my own code, and yes it was Apache Commons DateUtils.

I'm sorry I wasted your time.

Thanks

pmularien
Jul 18th, 2007, 09:29 AM
Glad you figured it out - it's not a waste of time, I'm sure someone will come up with this question again :)