Problem/Motivation

I got warning when validating form with datelist element that have either year, month, or day in #date_text_parts option.

Warning: checkdate() expects parameter 1 to be int, string given in Drupal\Component\Datetime\DateTimePlus::checkArray() (line 624 of /var/www/d9-dev/core/lib/Drupal/Component/Datetime/DateTimePlus.php)

Steps to reproduce

  1. Make a form with datelist element like below:
      $form['datelist_test'] = [
        '#type' => 'datelist',
        '#title' => 'Datelist test',
        '#date_part_order' => ['year', 'month', 'day'],
        '#date_text_parts' => ['year', 'month', 'day'],
      ];
  2. Fill non-numeric text on a form and submit.

Proposed resolution

Check that each value in array is numeric before call checkdate() in DateTimePlus::checkArray() .

Remaining tasks

n/a

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

n/a

Issue fork drupal-3241299

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hayashi created an issue. See original summary.

hayashi’s picture

Status: Active » Needs review
StatusFileSize
new950 bytes

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This will need a test case to show the issue as a next step.

Thanks

akram khan’s picture

StatusFileSize
new7.63 KB
new6.52 KB

added patch against 10.1.x and fixed some coding standard issues

nitin shrivastava’s picture

StatusFileSize
new7.84 KB
new7.64 KB

Fix command failures #7.

Abhisheksingh27’s picture

Status: Needs work » Needs review
StatusFileSize
new7.65 KB

Adding Reroll for 10.1.x

smustgrave’s picture

Status: Needs review » Needs work

Please see the tags before moving status this needs tests

_pratik_’s picture

StatusFileSize
new7.91 KB
new1.7 KB

CCF fixes found in #9

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dcam made their first commit to this issue’s fork.

dcam’s picture

Title: Warning in \Drupal\Component\Datetime\DateTimePlus::checkArray() » Handle invalid strings in DateTimePlus::checkArray()
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests

The patch from #2 was converted to an MR. Later patches included unrelated code style changes. I also added new cases to the existing test data provider.

smustgrave’s picture

Status: Needs review » Needs work

Think if someone incorrectly passes a string we should maybe log or throw an error so people know they need to fix their code.

dcam’s picture

Status: Needs work » Needs review

Think if someone incorrectly passes a string we should maybe log or throw an error so people know they need to fix their code.

I disagree. This is a validation function. It seems inappropriate for validation to throw an error if something doesn't validate. Logging, maybe, but I think even that's a stretch. It should be up to calling code to decide what to do if validation doesn't pass.

The only place in Core where this is called is in DateTimePlus::createFromArray() in the same class. ::createFromArray() throws an exception if validation failed. So it's doing the job.

smustgrave’s picture

I'll leave this one for others to review then.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Since no one else has reviewed in 2 months and my hesitation isn't something I feel so strong about to block I'm going to mark as the fix is straight forward.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs followup

At first I was confused by this, because I was expecting the date parts to allow valid month formats like "January", but it does indeed expect it to be integers.

There's an argument to be made that checkArray() should be a protected method, for the reason in #17.

Since int is expected, I am not sure is_numeric() goes far enough, since that could be floats and whatnot. It should be an int, or a string whose numeric value is an int.

I agree letting the caller handle throwing the exception is fine, and let's open a followup to discuss whether we should deprecate this being public and make it protected next major.

Thanks!

xjm’s picture

Title: Handle invalid strings in DateTimePlus::checkArray() » Properly return FALSE for invalid strings in DateTimePlus::checkArray()

Retitling to make it clear we're not "handling" them (which would be brittle and hide bugs). We're returning FALSE to trigger an exception in the caller. The original title had me all set to NW it but the approach is actually sound.

xjm’s picture

Title: Properly return FALSE for invalid strings in DateTimePlus::checkArray() » Properly return FALSE for invalid strings in DateTimePlus::checkArray() so that the correct exception is thrown
dcam’s picture

Status: Needs work » Needs review

Feedback has been addressed.

smustgrave’s picture

Mind opening the follow up ticket mentioned in #20

dcam’s picture

Issue tags: -Needs followup
dcam’s picture

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks rest LGTM!

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

alexpott’s picture

So this change results in a slightly different behaviour. It's a bit of an edge case but if you call the function with a float
On HEAD

> (string) \Drupal\Component\Datetime\DateTimePlus::createFromArray(['year' => 1975.5]);

   DEPRECATED  Implicit conversion from float 1975.5 to int loses precision in core/lib/Drupal/Component/Datetime/DateTimePlus.php on line 577.

= "1975-01-01 00:00:00 Europe/London"

With MR

> (string) \Drupal\Component\Datetime\DateTimePlus::createFromArray(['year' => 1975.5]);

   InvalidArgumentException  The array contains invalid values.

However I think is is okay because with the MR if you do

> (string) \Drupal\Component\Datetime\DateTimePlus::createFromArray(['year' => 1975.0]);
= "1975-01-01 00:00:00 Europe/London"

It's fine so it'll still accept floats but not ones that don;t represent an integer and passing a value that is not an integer to day, month or year makes no sense. However given this very slight behaviour change I'm going to only merge to main and 11.x

alexpott’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 22f99645e25 to main and 108cf90c63c to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 108cf90c on 11.x
    fix: #3241299 Properly return FALSE for invalid strings in DateTimePlus...

  • alexpott committed 22f99645 on main
    fix: #3241299 Properly return FALSE for invalid strings in DateTimePlus...

Status: Fixed » Closed (fixed)

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