I'm sorry if this has been reported or if it is not a bug. Possibly it is a config issue, but D8 documentation is killing me. I have seen numerous conflicting posts on how to set this and none seem to work - so guessing this is a bug.

I have a custom form with a field of type: date.

I have tried various methods to set a default value but none seem to work.

In the date form code i set:

    $form['date'] = [
      '#type' => 'date',
      '#description' => $this->t('Click in box to select date.'),
      '#default_value' => DrupalDateTime::createFromTimestamp(strtotime('2018-02-15')),
    ];

but, in the html this appears to give garbage:
<input type="date" data-drupal-selector="edit-date" aria-describedby="edit-date--description" data-drupal-date-format="Y-m-d" id="edit-date" name="date" 2018-02-15 00:00:00 America/Toronto class="form-date" />

i.e. the time string is outside any attr.

In the browser, for FF it shows a blank textfield and when i open the popup is shows default date as today (not the default date). In Chrome, it shows mm/dd/yyyy in the textfield and also shows today as default in the popup.

I have also tried setting in a form_alter like this:

$form['date']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp(strtotime($date));

and this also does not display correctly.

Oddly enough, I have a custom entity with a date field and it does work and display correctly. In the field.storage yml file for that field i see this:

type: datetime
settings:
datetime_type: date

Going from this i change my custom form to use type = datetime and sure enough that field does work as expected; except of course it now has the time selector part.

This leads me to think a couple things:
- there is a form #attr that can be set for a datetime field to disable the time portion (where is form element documentation?)
- setting form type as date does not work correctly (bug)

Am I the only one that misses the Forms API documentation that existed for D4, D5, D6 and D7 where it listed how to use form elements? Why does this not exist for D8? You can't seriously tell me these pages are supposed to replace this for D8:
- https://api.drupal.org/api/drupal/core%21core.api.php/group/form_api/8.4.x
- https://api.drupal.org/api/drupal/elements/8.4.x
- https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%...

CommentFileSizeAuthor
#10 2944552-10.patch913 bytesmpdonadio
#6 2944552-6.patch966 bytesbhanuprakashnani

Comments

liquidcms created an issue. See original summary.

tacituseu’s picture

Doc says:

#default_value: An array with the keys: 'year', 'month', and 'day'. Defaults to the current date if no value is supplied.

But this worked for me too (for '#type' => 'date'):
'#default_value' => '2018-02-15'.

liquidcms’s picture

Hmm.. perhaps i've been staring at the screen too long.. lol

In form code it works as you say (which i was sure i had tried multiple times).

but doesn't default to anything if not set, so I am using this now:

    $form['date']['#default_value'] = $date ? $date : date('Y-m-d');

where $date is in format 'Y-m-d'.

mpdonadio’s picture

Priority: Major » Normal

I think we need some (1) test coverage around the Datetime and Date render elements for this (2) adjust the docs to match what really is accepted and happens. There is too much confusion about how this works.

bhanuprakashnani’s picture

The code shown here is from the DateTimeFieldTest.php file in DateTime module?. Can I try creating a patch by changing the default value as discussed in the above comments by setting the default value to form['date']['#default_value'] = $date ? $date : date('Y-m-d');? Please let me know if the way I am going is right or not. Thank you.

bhanuprakashnani’s picture

Assigned: Unassigned » bhanuprakashnani
Status: Active » Needs review
StatusFileSize
new966 bytes

I have tried to make a change in the default value. Please review whether it's correct or not. If its wrong please let me know what is to be done. Please guide me. Thank you.

Status: Needs review » Needs work

The last submitted patch, 6: 2944552-6.patch, failed testing. View results

sophie.sk’s picture

Version: 8.4.x-dev » 8.7.x-dev

This is still an issue on latst 8.6 so updating the version.

The docs quite clearly state than an array of values is the right way to set the default value:

#default_value: An array with the keys: 'year', 'month', and 'day'. Defaults to the current date if no value is supplied.

But when I didn't provide a default value, the field defaulted to dd / mm / yyyy. When I provided a default value as an array, the field defaulted again to dd / mm / yyyy.

Only when I set $field['#default_value'] = date('Y-m-d'); did it start showing the correct date.

mpdonadio’s picture

Title: Default value does not work in date field. » Documentation for #default_value in Date form element is incorrect
Component: datetime.module » forms system
Assigned: bhanuprakashnani » Unassigned

Little housekeeping.

mpdonadio’s picture

Issue tags: +Documentation
StatusFileSize
new913 bytes

Here is a doc fix.

mpdonadio’s picture

Status: Needs work » Needs review
Issue tags: +DrupalCampNJ2019

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

akalata’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Yep this documentation is correct - regardless of the other render setting #date_date_format the expected date format is Y-m-d for the default value. What is also interesting is that for modern browsers #date_date_format setting seems meaningless because I think it is only used for a legacy stuff in date.es6.js - maybe we can file a follow up to address that because the current document about it in \Drupal\Core\Render\Element\Date seems misleading.

Committed and pushed dee0eb2880 to 8.8.x and 2adbbefe1f to 8.7.x. Thanks!

backported to 8.7.x as a docs fix.

  • alexpott committed dee0eb2 on 8.8.x
    Issue #2944552 by mpdonadio, bhanuprakashnani, liquidcms, Sophie.SK,...

  • alexpott committed 2adbbef on 8.7.x
    Issue #2944552 by mpdonadio, bhanuprakashnani, liquidcms, Sophie.SK,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

idebr’s picture

Version: 8.8.x-dev » 8.7.x-dev
idebr’s picture

#14 There was an earlier issue regarding the Date form element document being incomplete. I have repurposed it to update the documentation regarding its other than #default_value. This issue is available at #2836530: Update Date element documentation for #date_date_format and usage for other date types such as time