Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORM_SUBMIT: optional Date field #39

Closed
salamoun opened this issue Aug 8, 2012 · 4 comments
Closed

FORM_SUBMIT: optional Date field #39

salamoun opened this issue Aug 8, 2012 · 4 comments

Comments

@salamoun
Copy link

salamoun commented Aug 8, 2012

Hello,
I have ExtJS formpanel that has Date field. But this field is optional, so user can leave it empty.
But when form is saved over FORM_SUBMIT method I get the error:
Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startOfService'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value ''; nested exception is java.lang.IllegalArgumentException

I cannot figure how to set this field as optional. So when contains empty string I need field be ignored.

@ralscha
Copy link
Owner

ralscha commented Aug 8, 2012

Hi

Interesting problem. Does this field has an JsonDeserializer?
If yes you could handle this case in the deserializer.

How does the json look that the client is sending to the server?

Ralph

On Wed, Aug 8, 2012 at 5:01 PM, salamoun notifications@github.com wrote:

Hello,
I have ExtJS formpanel that has Date field. But this field is optional, so
user can leave it empty.
But when form is saved over FORM_SUBMIT method I get the error:
Failed to convert property value of type 'java.lang.String' to required
type 'java.util.Date' for property 'startOfService'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type java.lang.String to type
@org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value
''; nested exception is java.lang.IllegalArgumentException

I cannot figure how to set this field as optional. So when contains empty
string I need field be ignored.


Reply to this email directly or view it on GitHubhttps://github.com//issues/39.

@ralscha
Copy link
Owner

ralscha commented Aug 8, 2012

Created a small test application and this is indeed a problem.
The client sends something like this when the datefield is empty

extTID=5&extAction=formSubmitController&extMethod=handleFormSubmit&extType=rpc&extUpload=false&date=&remarks=

and then Spring somehow gets an empty String and tries to convert that to a
Date. There is nothing we can do in ExtDirectSpring because FORM_POST
methods are handled by Spring and also the conversion of the request
parameters into a bean.

I'm trying to find a solution in the Spring Forum.

Ralph

On Wed, Aug 8, 2012 at 6:00 PM, Ralph Schaer ralphschaer@gmail.com wrote:

Hi

Interesting problem. Does this field has an JsonDeserializer?
If yes you could handle this case in the deserializer.

How does the json look that the client is sending to the server?

Ralph

On Wed, Aug 8, 2012 at 5:01 PM, salamoun notifications@github.com wrote:

Hello,
I have ExtJS formpanel that has Date field. But this field is optional,
so user can leave it empty.
But when form is saved over FORM_SUBMIT method I get the error:
Failed to convert property value of type 'java.lang.String' to required
type 'java.util.Date' for property 'startOfService'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type java.lang.String to type
@org.codehaus.jackson.map.annotate.JsonSerialize java.util.Date for value
''; nested exception is java.lang.IllegalArgumentException

I cannot figure how to set this field as optional. So when contains empty
string I need field be ignored.


Reply to this email directly or view it on GitHubhttps://github.com//issues/39.

@ralscha
Copy link
Owner

ralscha commented Aug 8, 2012

Found a solution in the Spring forum. It looks like you have to add a
@InitBinder method.

I added this code in the same @controller class where the FORM_POST is
located and it worked.

@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
CustomDateEditor editor = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, editor);
}

Here the link to the post from the Spring forum:
http://forum.springsource.org/showthread.php?95577-How-do-I-make-Spring-accept-an-empty-value-for-a&highlight=form+submit+empty+date+field

Hope this works in your project as well.
Ralph

@salamoun
Copy link
Author

salamoun commented Aug 8, 2012

It works. Perfect solution. Thank you.

@ralscha ralscha closed this as completed Sep 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants