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

parsing string to date #217

Closed
josegranja opened this issue Jan 18, 2014 · 1 comment
Closed

parsing string to date #217

josegranja opened this issue Jan 18, 2014 · 1 comment

Comments

@josegranja
Copy link

Hello,

I'm using FOSRestBundle with JMS Serializer. I bind request parameters using ParamConverter:

@ParamConverter("person", converter="fos_rest.request_body")

I see that this ParamConverter is calling this JMS Serializer internally. I have a problem with dates. For example I have the property:

/**
* @annotation\Expose
* @annotation\Type("DateTime<'Y-m-d'>")
*
*/
private $birthday;

the serialization happens as expected on most of the scenarios and only fails if the format is wrong. for example: 2014-2222-02. However is doesn't fail when the month number is out of range: 2014-30-02.

I have tracked down the error to the class DateHandler and the method: parseDateTime. What is happening is that the conversion: \DateTime::createFromFormat($format, (string) $data, $timezone); will never throw an exception and return always true if the month number or month day is out of range. What it would do is save this as a warning and try to guess the date.

This warnings can be checked through \DateTime::getLastErrors(). I can see that this rightly done in DateTimeToStringTransformer object reverseTransform method.

Does that sound ok to you? Let me know what you think.

many thanks,

jose

@goetas
Copy link
Collaborator

goetas commented Mar 21, 2017

You have to implement a custom date type and do the parsing of the wrong datefromat by your self.

/**
* @Annotation\Expose
* @Annotation\Type("MyDateTime")
*
*/
private $birthday;

And provide a type handler for MyDateTime

@goetas goetas closed this as completed Mar 21, 2017
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