I think this is probably a bug, but feel free to educate me on the intended behavior in case I missed something.
I think of an optional field as one that, if ignored on the node creation form, receives some null value (e.g. empty string). My theme can then use the value saved in the field to determine whether or not a value was entered for that field, so it can handle the two cases differently.
I would like to be able to do this with a date field, but it does not appear to be possible. Even explicitly entering a blank value or selecting the blank and 00 items from the select menus does not result in a database value that tells me no date was entered.
When I uncheck the "required" box for an ISO date field I get the following behavior for the different input widgets, where each of them comes up initially with the current date/time filled in:
(1) select list - select blank item in month menu, leave day and year fields blank, hour 00, minute 00 and blank am/pm => saves it as midnight of the current date.
(2) text field with strtotime validation - leave it blank => saves it as midnight of current date
(3) text field with jscalendar - leavie it blank => validation error "The text '' is not a valid date."
I haven't checked how it works with the datestamp field. I suppose it's likely to have the same behavior.
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | Screenshot_14.gif | 13.35 KB | jarea |
| #11 | optional-dates_1.patch | 8.65 KB | wrunt |
| #10 | optional-dates_0.patch | 7.29 KB | wrunt |
| #9 | optional-dates.patch | 7.29 KB | wrunt |
Comments
Comment #1
RayZ commentedThis may be an issue with CCK fields in general: http://drupal.org/node/72135
Comment #2
karens commentedThere really is no way to have a blank value for a date field. A timestamp field requires an integer value and a value of 0 is a valid date (1/1/1970). The iso field could technically contain a blank without creating any database error, but the module is set up to at least require a year so there is a way to validate input data and keep out things that are completely invalid as dates.
I just don't see any way it is possible to allow for an 'empty' date field, at least not easily.
Comment #3
karens commentedTo clarify further, I did intend that you can use the iso date to store just a month and year or just a year, but it does require at least a year. Storing partial dates like that is not completely tested -- should work in theory but may need some debugging.
Comment #4
RayZ commentedWhy not allow NULL values in the database?
Comment #5
karens commentedI'm changing this to a feature request since the module is behaving as designed -- although you might argue that it should have been designed differently :-). I can see a use for some way to make date fields optional, but it's a lower priority than getting the basic functionality working, so I'm not going to do anything with this immediately but will keep it out here as a feature request.
This is a non-trivial fix -- it may involve a database change and will require significant changes to the way that validation is handled.
Comment #6
RayZ commentedJust for reference, Making end date 'optional' and vaildation of input in the Event issue queue, includes a patch to implement it in that context. No idea what approach is taken and if it is relevant here.
Comment #7
Justin Freeman commentedWe came up against a problem using a custom content type (CCK) and the date field module. Because the date field always stores a date, it is counter-intuitive when the date field is optional.
In our particular use case, if the content type "status report" has an optional field: date completed. Then this must not be set unless the user explicitly sets the value. Currently, date completed will be automatically set to a value on submit, without the user knowing it. And thereby marking the "status report" as completed.
Regardless of the database/table design, users need a way to leave a date field blank. Otherwise, weird workarounds will have to be used.
Would really like this "feature request" progressed. Happy to submit a patch if you will review :)
Comment #8
karens commentedI'd be more than happy to review a patch. This is something I just haven't had time to get to. The dicey thing is going to be figuring out how to do this in 4.7 vs cvs since cvs is in the process of undergoing big changes and it's not completely clear yet what the changes will be (all related to the fact that parts of cck have been added to core). I'm open for suggestions...
Comment #9
wrunt commentedHere's a patch on greenrenault's behalf.
This makes date fields optional if they're not marked as required. It works for text, jscalendar, and select lists. If a field isn't required then it will default to blank. If a field is required then it will default to the current date.
I haven't tested extensively, but it seems to work.
Comment #10
wrunt commentedThis is a revised patch with a small fix for assigning the values of selects properly.
Comment #11
wrunt commentedThere was a bug in that last patch. I've fixed that, and updated to patch the latest cvs.
Comment #12
karens commentedI need this capability myself now so this has risen to the top of my priority list :-)
I'm not sure the patch catches all the potential issues/problems, but it's going to be a handy starting point for me (thanks!). This is more complicated than it might first appear, so I may not come up with a solution right away, but, as I said, I need to get this working myself, so I am plugging away at it.
Comment #13
karens commentedI can't get the patch to apply. If you can, please re-roll it, otherwise I'll just try to do this manually.
Comment #14
karens commentedDon't worry about re-rolling the patch, I'm just going through it by hand (which is a good exercise anyway). Looks like this patch catches most of the changes that are needed, I'm adding a couple other small changes and I need to do some more testing before committing it.
Comment #15
wrunt commentedGreat! Let me know if you need me to test your version. Sorry about the patch not applying, I'm not sure why that would be. I made the patch by first doing 'cvs up -dP' on the contrib tree, then adding my changes, then running 'cvs diff -uF^f modules/date > optional-dates.patch'. That should have worked, right?
Comment #16
karens commentedOK, I have this almost working but am stuck on something being done by content.module. If I try to create a NULL value, content.module converts it to '' for an ISO feld or 0 for a timestamp field instead of storing it as NULL. The blank value for the ISO field still works sort of OK, but the zero value for the timestamp field is completely wrong. The function that needs to be altered in content.module is content_field(). I am still trying to figure out what fix would allow a NULL value to be saved, but if anyone else can see what fix is needed it would be very helpful.
Comment #17
karens commentedSee my proposed patch for the content module at http://drupal.org/node/85929. Reviews are needed to move it along!!
Comment #18
karens commentedThe cvs version of the date module now has my preliminary attempt at making optional date fields work correctly. I used the patches provided by greenrenault and wrunt as a starting point (thanks a bunch! I forgot to credit you guys on the commit message, but I really appreciate it). There are database changes involved and you must also apply the patch to the content module in the link above.
To update your database and get things working, do the following (in this order):
1. Go to each date field you have in your database now and pull it up in admin > content > content_types. Double check the settings (there are changes) and save it. Do this before updating the database because the new settings will be used to save values in the update.
2. Go to update.php and update your database. There are 2 updates, 5 and 6. The second is not related to this issue but will be needed for another issue and I don't want people to have to update the database twice, so I'm doing it now. (I'm adding in an offset value for the timezone field so we can properly account for timezone adjustments when searching views, and removing timezone columns for dates that don't use date-specific timezones since they take of lots of unnecessary storage space).
3. Try adding and editing optional date fields.
This is basically working but really needs testing and review. Also, please post comments on the content module patch issue so we can get that patch committed (or fixed if it's not working right).
I debated whether or not to commit this since it's not completely working right, but the current behaviour is completely broken, so it's at least progress :-)
Comment #19
karens commentedAlso, I reiterate, this is not ready for primetime yet! Don't use this on a production site until we get it debugged!
Comment #20
karens commentedAlso, I forgot to say that this cvs version is still targeted for drupal 4.7, not 5.0, since the content module isn't yet ported.
Comment #21
jarea commentedI uploaded the new CVS version and patched the content module as instructed.
The first date field that I tried to open and update generated the following message
"An illegal choice has been detected. Please contact the site administrator."
In the configuration settings, there is now a required Widget field with nothing to activate or enter ... just Widget: *
Everything else looks pretty much the same. For the record, this date field has the required box checked.
Since mine is just a testbed site, I can play for awhile without having to bring back to old date module.
Let me know what you think.
Comment #22
karens commentedI haven't run into this and can't replicate it. You're talking about trying to use the field settings screen, right? What kind of field was it and what kind of widget? A screen shot would be nice if you can.
Try creating a new field and see if that works. Maybe there's some combination of existing field that isn't getting transitioned right.
Comment #23
jarea commentedScreenshot is attached. I tried adding a new test date field and the date field type is no longer available. So I went to admin>modules thinking that maybe the new date module that I uploaded had not been activated. It was checked. When I unchecked it, I got this error message.
I went back and uploaded the old date module and tried to resubmit admin>modules and received the same error message.
Comment #24
karens commentedThere are some known caching problems, so first let's try emptying the cache table and see what that does. You need to completely delete everything in that table, just going to the modules page and saving it won'tdo the job.
Comment #25
karens commentedAre you working on something with existing data, or is this a clean install? The missing argument message usually appears when people try to update the content module with changes that were made in August. Was your content module uptodate before you started this? I am thinking that some content.module updates got mixed into this.
Comment #26
karens commentedI'm adding something to the instructions for testing this change. Be sure that all content module updates have been applied and that cck is working right before attempting to test these changes. The last content module update created lots of problems for many people and anyone who hasn't already made that update and gotten things working again will be able to tell whether this module is working right.
The problem is that anyone who didn't realize there was an update needed to the content module who tries to apply this update is going to get both at once, and they won't have any way to sort out what problems arose from one versus the other.
Comment #27
karens commented@jarea, do you have a backup of where you were before you started? If so, please go back to it and start over, that will be way less time-consuming than trying to sort out problems introduced by the content module update. Use the old date module and get the content module updated and working correctly. Once you know all the content module updates are working right, then try the latest version of the date module.
Comment #28
jarea commentedWill do. Thanks Karen.
Comment #29
karens commentedI've gone ahead and committed these changes to the 4.7 version as well as the cvs version of the date module. I've been using it in a couple places and am pretty comfortable it is working correctly except for the patch needed to the content module to properly handle null fields, which I hope will be committed soon.
Comment #30
drewish commentedwait, so you committed this to 4.7 but the cck patch hasn't hit yet? is that why the date field stoped working after i updated it?
Comment #31
drewish commentedhumm, my problem seems to be specifically with the select boxes... not sure if it's related.
Comment #32
karens commentedUgh! I meant to go back and commit the patch. It is now. I've got too many balls in the air.
I doubt that was your problem, though, it wouldn't come into play unless you were storing empty values in your fields. If you describe what you're seeing, I may be able to fix it. This patch should give you blank values when you add a new, non-required field, instead of defaulting to the current date, and will then let you save a record with that empty entry.
Comment #33
(not verified) commentedComment #34
geek-merlinsub
Comment #35
kthull@axel.rutz this still works, you just need to dig deep to find the option. When you edit your date field settings, go under "More settings and values" and expand the "Default values" field group. There you can set the default to "No default value"