Thanks to visit codestin.com
Credit goes to github.com

Skip to content

how to handle when use submit empty datetime? #780

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

Open
videni opened this issue Jul 27, 2018 · 9 comments
Open

how to handle when use submit empty datetime? #780

videni opened this issue Jul 27, 2018 · 9 comments

Comments

@videni
Copy link

videni commented Jul 27, 2018

currently, when user submit an empty datetime, we will get error

The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.
@videni
Copy link
Author

videni commented Jul 31, 2018

@dunglas , seems no one has this trouble,how do you solve this please?

@Lobosque
Copy link

Lobosque commented Aug 9, 2018

@videni I have exactly the same problem. Were you able to solve this?

@videni
Copy link
Author

videni commented Aug 10, 2018

@Lobosque , I create a dedicated denormalizer which will unset the key of the date time attribute if user submit an empty date time. as the example below, currently I hard coded the key 'expires_at' for I only have one entity to deal with, if you have several attributes you can add configuration yourself.

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

class EmptyDateTimeDenormalizer implements DenormalizerInterface
{
    private $decorated;

    public function __construct(
        ItemNormalizer $decorated
    ) {
        $this->decorated = $decorated;
    }

    public function denormalize($data, $class, $format = null, array $context = [])
    {
        $data = $this->removeEmptyDateTime($data, $class);

        return $this->decorated->denormalize($data, $class, $format, $context);
    }

    private function removeEmptyDateTime($data, $type)
    {
        if ($type == User::class && isset($data['expires_at']) && $data['expires_at'] == '') {
            unset($data['expires_at']);
        }

        return $data;
    }
}

@Lobosque
Copy link

@videni thank you for sharing your solution.
Even though we can work around the problem with a custom denormalizer, I still think this it should accept empty values and leave the validator to handle if it is valid or not.

@byhoratiss
Copy link

This is happening to me too. Can't find a way to solve this.
I'll try your solution @videni, thank you!

@sylfabre
Copy link

@dunglas Would you accept a PR to make API Platform accept empty datetime and denormalize them as null?

@kamil-p
Copy link

kamil-p commented May 17, 2020

Hey Guys,

I have handle it that way: https://stackoverflow.com/questions/61852792/api-platform-datetimenormalizer-not-allowing-null/61852793#61852793

@carlospauluk
Copy link

Same problem here at 2021.

@ka4kok
Copy link

ka4kok commented Jul 9, 2021

Just set for setter function with ? and param DateTime|null. It works!

/**
* @param DateTime|null $dateAt
*/
public function setDateAt(?DateTime $dateAt): void
{
$this->dateAt = $dateAt;
}

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

7 participants