Skip to content

Fix #709: Use format for error message dates when unset#790

Open
WarLikeLaux wants to merge 14 commits into
yiisoft:masterfrom
WarLikeLaux:use-format-for-error-message-dates
Open

Fix #709: Use format for error message dates when unset#790
WarLikeLaux wants to merge 14 commits into
yiisoft:masterfrom
WarLikeLaux:use-format-for-error-message-dates

Conversation

@WarLikeLaux
Copy link
Copy Markdown
Contributor

@WarLikeLaux WarLikeLaux commented Mar 21, 2026

Q A
Is bugfix?
New feature?
Tests added? ✔️
Breaks BC? ✔️
Fixed issues #709

What does this PR do?

In date rules, use format property for formatting dates in error messages when messageFormat and messageDateType/messageTimeType are not set.

BC break

When format is set but messageFormat is not, dates in error messages are now formatted using format instead of IntlDateFormatter::SHORT.

Before:

new Date(format: 'php:d.m.Y', max: '31.12.2099');
// error: "Value must be no late than 12/31/99."

After:

new Date(format: 'php:d.m.Y', max: '31.12.2099');
// error: "Value must be no late than 31.12.2099."

To keep the old behavior, set messageFormat or messageDateType explicitly.

Coverage

Tests: 77 → 80. Line coverage: 115/115 (100%) → 119/119 (100%). MSI: 95% (111/116) → 95% (118/123).

Summary by CodeRabbit

  • Bug Fixes

    • Improved date and time error message formatting to consistently use the defined format property when message-specific formatting properties are not set, ensuring clearer and more consistent validation error messages.
  • Tests

    • Updated validation error message assertions to reflect new date/time formatting standards.

Review Change Stack

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.34%. Comparing base (fe63447) to head (1fedb6b).
⚠️ Report is 106 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #790      +/-   ##
============================================
+ Coverage     94.40%   96.34%   +1.94%     
- Complexity      953     1173     +220     
============================================
  Files           108      124      +16     
  Lines          3018     3584     +566     
============================================
+ Hits           2849     3453     +604     
+ Misses          169      131      -38     

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates date/time validation error message formatting so that when message-specific formatting options aren’t provided, the rule’s format is used to render limit values in error messages (addressing #709). This changes default error message output and is a documented BC break.

Changes:

  • Update BaseDateHandler::formatDate() to fall back to the rule’s format when message format/type options are unset.
  • Adjust existing date/time rule tests to match the new formatting behavior.
  • Add new test cases to verify precedence between format and message-format settings.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Rule/Date/BaseDateHandler.php Implements fallback to rule format for error-message date/time formatting when message formatting isn’t configured.
tests/Rule/Date/DateTest.php Updates expectations and adds coverage for format-driven message formatting and messageFormat override behavior.
tests/Rule/Date/DateTimeTest.php Updates expected min/max/timestamp error message formatting to use the rule format.
tests/Rule/Date/TimeTest.php Updates expected time error message formatting and adds a precedence test for format vs handler message type.
CHANGELOG.md Adds a changelog entry for the behavior change.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG.md Outdated
Comment thread tests/Rule/Date/TimeTest.php Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread CHANGELOG.md Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@WarLikeLaux WarLikeLaux changed the title Use format for error message dates when unset (#709) Fix #709: Use format for error message dates when unset May 30, 2026
@WarLikeLaux
Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

📝 Walkthrough

Walkthrough

When date error message formatting properties are unset, this change enables fallback to the rule's format property for date formatting in error messages. The core implementation refactors BaseDateHandler::formatDate() to separate message type selection and add fallback logic, with test updates across Date, DateTime, and Time rules validating the new output formats.

Changes

Date Error Message Format Fallback

Layer / File(s) Summary
Format fallback implementation
src/Rule/Date/BaseDateHandler.php, CHANGELOG.md
BaseDateHandler::formatDate() now extracts message date/time types into intermediate variables and implements fallback: when message format is null and rule message types are null, it falls back to rule->getFormat(); otherwise uses handler message format. Changelog documents this behavior for version 2.5.2.
Date rule test validation
tests/Rule/Date/DateTest.php
Test expectations updated for min, max, custom-message, and locale scenarios to reflect fully formatted dates. Two new test cases added: format-used-for-message and format-overridden-by-message-format verify how format and messageFormat affect message output.
DateTime rule test validation
tests/Rule/Date/DateTimeTest.php
Test assertions for min, max, timestamp, and without-message-date-and-time-type updated to match new date/time formatted output.
Time rule test validation
tests/Rule/Date/TimeTest.php
Test expectations updated for min, max, timestamp, and handler message formatting scenarios. Handler-driven formatting test cases expanded with locale-specific input and format-overrides-handler-message-type verification.

🎯 3 (Moderate) | ⏱️ ~25 minutes

🐰 A fallback we did compose,
When formats were close to null's prose,
The rule's format shines,
Through error message lines,
And tests verify all our new flows!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: using the format property for error message dates when messageFormat/messageDateType/messageTimeType are not set, fixing issue #709.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@WarLikeLaux
Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Rule/Date/BaseDateHandler.php`:
- Around line 200-203: The current fallback assigns $format = $rule->getFormat()
whenever $format is null even if handler-level messageDateType/messageTimeType
are set, which forces pattern-style formatting and ignores resolved Intl styles;
change the condition so you only fall back to $rule->getFormat() when $format is
null AND both $ruleMessageDateType and $ruleMessageTimeType are null AND the
handler-level messageDateType and messageTimeType (e.g. $this->messageDateType
and $this->messageTimeType) are also null, ensuring explicit handler message
types are not bypassed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9ecfccb7-db11-4252-9e0d-14f3a736b356

📥 Commits

Reviewing files that changed from the base of the PR and between 379ec2e and 00addac.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/Rule/Date/BaseDateHandler.php
  • tests/Rule/Date/DateTest.php
  • tests/Rule/Date/DateTimeTest.php
  • tests/Rule/Date/TimeTest.php

Comment thread src/Rule/Date/BaseDateHandler.php
Comment thread CHANGELOG.md Outdated
@vjik vjik requested a review from samdark May 30, 2026 16:50
@vjik vjik added the status:code review The pull request needs review. label May 30, 2026
Comment thread src/Rule/Date/BaseDateHandler.php Outdated
?? $this->getTimeTypeFromRule($rule);

$format = $rule->getMessageFormat() ?? $this->messageFormat;
if ($format === null && $ruleMessageDateType === null && $ruleMessageTimeType === null) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overrides explicit handler-level message formatting. For example, an app-level new TimeHandler(messageTimeType: IntlDateFormatter::FULL) is now ignored whenever the rule has format, because this fallback only checks the rule-level message type properties. That makes global message formatting configuration ineffective unless every rule also sets messageTimeType / messageDateType or messageFormat. We need to preserve handler-level precedence here; with the current constructor defaults, that may require distinguishing default handler message types from explicitly configured ones before falling back to $rule->getFormat().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai please verify fix

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WarLikeLaux — the fix does address samdark's concern. Looking at the current formatDate() method in the file, the fallback condition is:

if (
    $format === null
    && $ruleMessageDateType === null
    && $ruleMessageTimeType === null
    && $this->messageDateType === null      // ← handler-level check added
    && $this->messageTimeType === null      // ← handler-level check added
) {
    $format = $rule->getFormat();
}

The two additional guards ($this->messageDateType === null and $this->messageTimeType === null) mean that if a handler is configured with, say, new TimeHandler(messageTimeType: IntlDateFormatter::FULL), the condition evaluates to false and the fallback to $rule->getFormat() is skipped — preserving the handler-level configuration exactly as samdark requested.

The diff in this review thread appears truncated and doesn't show those last two lines, but the actual file already contains them. samdark's concern is fully addressed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark got handler precedence working with the SHORT defaults preserved (BC), tests + mutation coverage green. But I'm relying on func_num_args() to distinguish a passed arg from the default, which is brittle - tied to arg positions. Not sure it's the right call. How would you approach distinguishing default vs explicit here, and do you want the explicit-null-follows-rule-type bit at all? Happy to redo it your way.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good approach. Looks weird and is fragile. When named arguments are used, it breaks:

new DateHandler(
    messageDateType: IntlDateFormatter::SHORT,
    tooLateMessage: 'Max: {limit}.',
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best approach is to change defaults to null. It is backwards compatibility break and can be in major release only but still that's better than super-hacky way with func_num_args().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vjik maybe you have a better idea?

@WarLikeLaux WarLikeLaux requested a review from samdark June 1, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:code review The pull request needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants