Easyadminbundle: Support "empty" date and datetime values like 0000-00-00 00:00:00

Created on 27 Mar 2015  路  19Comments  路  Source: EasyCorp/EasyAdminBundle

Hi first thanks very much for your project, it is very nice ! :)

I have a situation where I have fields like this on an Entity :

  • onlineAt (datetime)
  • offlineAt (datetime)
  • online (boolean)

The ones with datetimes can be filled or not.

With the current state of the project when I do a new Item with the admin, it sets the DateTime like this :
2010-01-01 00:00:00

This is annoying for me as I expect this value to be this by default (nothing filled by the user) :
0000-00-00 00:00:00

So I have to go manually correct the value via phpmyadmin like tool for each entry, which is not very productive :)

Can you please fix this behavior ?

feature hasPR

Most helpful comment

This has been finally fixed in #607 and it will available in the next stable version of the bundle to be released very soon.

All 19 comments

Shouldn't we rather allow to set a datetime field to null from the form instead ? (with a checkbox disabling the datetime widget)
Eventually, a new default option for datetime type in forms configuration, allowing to provide the default value:

easy_admin:
    entities:
        Customer:
            class: AppBundle\Entity\Customer
            form:
                fields:
                    - { property: 'dateOfBirth', default: '0000-00-00 00:00:00' }

Note you're already able to do this by specifying a default date from your entity constructor (But might be undesired in certain cases).

In fact on the edit view this "empty" value will yield an critical error :

CRITICAL - Uncaught PHP Exception SymfonyComponentFormExceptionTransformationFailedException: "Unable to transform value for property path "[date]": This year is invalid"

It seems that "empty" date / datetime values like _0000-00-00 00:00:00_ or _NULL_ values aren't properly handled. I don't know if it's _easy admin related_ or _symfony form related_ right now.

AFAICT, it's a php DateTime "issue" for the '0000-00-00 00:00:00', but it can be workaround for our purpose by a custom form type allowing to set the date to null from the form I guess.

Just set the default value to null and the date will render as null in the back-end.
Why would you need a date with a '0000-00-00 00:00:00' value?

@Pierstoval : seems quite common: http://stackoverflow.com/questions/12482560/default-date-0000-00-00-000000-or-null.

But I personally never used this way.

Hmm.. Indeed, and with @sylvaincombes's usage, I think it might be better to keep the possibility to use operations and date comparisons.
I'm gonna investigate a little for this issue

After some investigation, I think we can close the issue, because it's really not an EasyAdmin problem, and solving this kind of problem might be a too much big thing, and IMO, this problem is easily circumvented.

For the rest, there are two solutions for you @sylvaincombes (and for other people with this problem):

  • Create a custom Doctrine type that will probably be your own class extending DateTime, in order to handle properly the 0000-00-00 00:00:00 value, which is probably the most efficient but the most complex.
  • Use the null value and when you need to make queries with Doctrine, don't forget to replace WHERE {date_field} < :date with WHERE {date_field} < :date OR {date_field} IS NULL, because it's the only way to do this operation, which is really the easy and fast way.

@Pierstoval : I agree this is not an EasyAdmin problem, but there is still an issue here: easyadmin forms (new & edit actions) do not allow to set a datetime field to null.
The 0000-00-00 00:00:00 isn't a valid date and caused similar issues on other languages (java at least), nor it is something selectable in a datetime widget anyway.
But null should eventually be.

If the date is nullable, every <select> will have a default empty value available, so the problem will not be triggered.

What we can do is propose a custom date and datetime widget with a datepicker, like the one there is in phpmyadmin, for instance.

If the date is nullable, every