Skip to content

fix(helper): Accept date formats without a year or month - #1883

Open
zigzagdev wants to merge 2 commits into
Respect:mainfrom
zigzagdev:fix/date-without-year
Open

zigzagdev wants to merge 2 commits into
Respect:mainfrom
zigzagdev:fix/date-without-year

Conversation

@zigzagdev

Copy link
Copy Markdown

Summary

Date now accepts valid input for formats that have a day but no year or month, such as d/m.

Motivation

Date accepts formats like d/m, m/d, d and F j, but valid input for them is always rejected.

(new Date('d/m'))->evaluate('15/03')->hasPassed; // false
(new Date('F j'))->evaluate('March 5')->hasPassed; // false

When a field is missing from the format, date_parse_from_format() returns false for it.
isDateInformation() cast these values to 0, so checkdate() always failed.
Formats without a day already defaulted missing values to 1, so the two branches behaved differently.

What I have done

In CanValidateDateTime::isDateInformation(), a missing month or year now defaults to 1 when the format has a day.
Only false is replaced, so explicit zeros such as year 0000 or month 00 are still rejected as before.
Impossible dates are still rejected by the existing round trip check.

Test plans

Added the following cases to DateTest.

  • Valid: d/m with 15/03, m/d with 03/15, d with 15 and F j with March 5
  • Invalid: d/m with 31/02

Test Results

$ vendor/bin/phpunit --testsuite=unit --filter DateTest --testdox
PHPUnit 12.5.33 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.5.12
Configuration: /tmp/app/phpunit.xml.dist

..............................                                    30 / 30 (100%)

Time: 00:00.073, Memory: 40.00 MB

Date (Respect\Validation\Validators\Date)
 ✔ Should validate valid input with data set #0
 ✔ Should validate valid input with data set #1
 ✔ Should validate valid input with data set #2
 ✔ Should validate valid input with data set #3
 ✔ Should validate valid input with data set #4
 ✔ Should validate valid input with data set #5
 ✔ Should validate valid input with data set #6
 ✔ Should validate valid input with data set #7
 ✔ Should validate valid input with data set #8
 ✔ Should validate invalid input with data set #0
 ...
 ✔ Should validate invalid input with data set #17
 ✔ Should validate invalid input with data set #18
 ✔ Should throw an exception when format is not valid with data set #0
 ✔ Should throw an exception when format is not valid with data set #1

OK (30 tests, 30 assertions)

When a format had a day but no year or month, the missing values were
cast to 0, so checkdate() always failed and valid input such as "15/03"
for "d/m" was rejected. Missing values now default to 1, while explicit
zeros like year 0000 are still rejected.
Cover "d/m", "m/d", "d" and "F j" with valid input, plus an impossible
date ("31/02" for "d/m") that must still be rejected.
@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.18%. Comparing base (ecccef1) to head (beeffdb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #1883   +/-   ##
=========================================
  Coverage     97.18%   97.18%           
- Complexity     1096     1098    +2     
=========================================
  Files           198      198           
  Lines          2555     2558    +3     
=========================================
+ Hits           2483     2486    +3     
  Misses           72       72           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant