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 :
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 ?
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):
0000-00-00 00:00:00 value, which is probably the most efficient but the most complex.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
I didn't experienced this behavior with my last tests, because empty_value isn't set for the datetime form type.
Anyway I don't think this is really handy. I was simply suggesting a checkbox next to the datetime fields in order to disable all the select inputs at once and submit null, but there's still other issues with the basic datetime choice widget (unable to select any year for example, the range is limited).
So yes, maybe it's time to choose a date & time picker implementation. :)
I'm encountering the same issue.
If the field is nullable, EasyAdmin should add an "empty" value we can select. Even if it is not an issue from EasyAdmin, it is a feature it should have. Either through a checkbox or by using an HTML5 date widget we can leave empty.
A typical use case is when editing an entity with a "soft deleteable" filter. Actually, you will have "sneakily" a date set, with catastrophic consequences: your entity is now (soft) deleted...
When you have a soft deleteable filter on your entities, you should never show this field anywhere in your app. Actually these filters are mostly here to avoid data loss, so they're handled by listeners only.
A solution might be the use of an HTML5 date field, or a JS/jQuery datepicker which returns a string, so you don't use your classic setters, but create a specific setter that converts the string into a DateTime object.
I still think it's an issue from outside EasyAdmin and we should not handle this "manually", because every entity can handle its date or datetime properties differently.
You may want to offer some super-hero-admin the ability of cancelling a soft delete. Then you have to allow them to set this property to null.
I think EasyAdmin should have a sane default. Forcing people to set a value when editing an entity is not IMHO.
Then if you want this super-hero-admin, you're forced to modify the formBuilder manually depending on current user's role, so you can add some lines of code in your controller to check the date's consistency with the DBMS. It's an issue that may have to be solved in the Form component (maybe a single option can be added to handle this), so available only on SF2.8+ (at the moment), or at a lower level, in Doctrine, or at an even lower level, in PHP itself, so its DateTime class can use dates like 0000-00-00 00:00:00.
By the way, nullable dates are handled by EasyAdmin IIRC.
So, is this finally implemented? I see this but my entity field is nullable and no _"make null"_ checkbox appears. I can't make a date null.
Thanks
@javiereguiluz @ogizanagi: so, is this finally implemented? I see this but my entity field is nullable and no _"make null"_ checkbox appears. I can't make a date null.
Thanks
@nzurita : No, it hasn't yet. You can consult my own reasons in https://github.com/javiereguiluz/EasyAdminBundle/issues/344#issuecomment-107942821. I haven't thought more about this problem since. :/
The only way as of version 1.9.1 to set a datetime to NULL is to leave all the fields empty one by one. It was like this before:

And now this:

No "one click null setter" yet.
This has been finally fixed in #607 and it will available in the next stable version of the bundle to be released very soon.
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.