Skip to content

Commit 726434c

Browse files
[12.x] Fixes how the fluent Date rule builder handles date_format (#55052)
* Fixed adding a format to the fluent date rule builder * Conformed to StyleCI * Take initial rules from previous PR * Added tests for specific dates with custom date format * Update Date.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3503a48 commit 726434c

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/Illuminate/Validation/Rules/Date.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@ class Date implements Stringable
1212
{
1313
use Conditionable, Macroable;
1414

15+
/**
16+
* The format of the date.
17+
*/
18+
protected ?string $format = null;
19+
1520
/**
1621
* The constraints for the date rule.
1722
*/
18-
protected array $constraints = ['date'];
23+
protected array $constraints = [];
1924

2025
/**
2126
* Ensure the date has the given format.
2227
*/
2328
public function format(string $format): static
2429
{
25-
return $this->addRule('date_format:'.$format);
30+
$this->format = $format;
31+
32+
return $this;
2633
}
2734

2835
/**
@@ -121,7 +128,7 @@ protected function addRule(array|string $rules): static
121128
protected function formatDate(DateTimeInterface|string $date): string
122129
{
123130
return $date instanceof DateTimeInterface
124-
? $date->format('Y-m-d')
131+
? $date->format($this->format ?? 'Y-m-d')
125132
: $date;
126133
}
127134

@@ -130,6 +137,9 @@ protected function formatDate(DateTimeInterface|string $date): string
130137
*/
131138
public function __toString(): string
132139
{
133-
return implode('|', $this->constraints);
140+
return implode('|', [
141+
$this->format === null ? 'date' : 'date_format:'.$this->format,
142+
...$this->constraints,
143+
]);
134144
}
135145
}

tests/Validation/ValidationDateRuleTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testDefaultDateRule()
2424
public function testDateFormatRule()
2525
{
2626
$rule = Rule::date()->format('d/m/Y');
27-
$this->assertEquals('date|date_format:d/m/Y', (string) $rule);
27+
$this->assertEquals('date_format:d/m/Y', (string) $rule);
2828
}
2929

3030
public function testAfterTodayRule()
@@ -49,30 +49,45 @@ public function testAfterSpecificDateRule()
4949
{
5050
$rule = Rule::date()->after(Carbon::parse('2024-01-01'));
5151
$this->assertEquals('date|after:2024-01-01', (string) $rule);
52+
53+
$rule = Rule::date()->format('d/m/Y')->after(Carbon::parse('2024-01-01'));
54+
$this->assertEquals('date_format:d/m/Y|after:01/01/2024', (string) $rule);
5255
}
5356

5457
public function testBeforeSpecificDateRule()
5558
{
5659
$rule = Rule::date()->before(Carbon::parse('2024-01-01'));
5760
$this->assertEquals('date|before:2024-01-01', (string) $rule);
61+
62+
$rule = Rule::date()->format('d/m/Y')->before(Carbon::parse('2024-01-01'));
63+
$this->assertEquals('date_format:d/m/Y|before:01/01/2024', (string) $rule);
5864
}
5965

6066
public function testAfterOrEqualSpecificDateRule()
6167
{
6268
$rule = Rule::date()->afterOrEqual(Carbon::parse('2024-01-01'));
6369
$this->assertEquals('date|after_or_equal:2024-01-01', (string) $rule);
70+
71+
$rule = Rule::date()->format('d/m/Y')->afterOrEqual(Carbon::parse('2024-01-01'));
72+
$this->assertEquals('date_format:d/m/Y|after_or_equal:01/01/2024', (string) $rule);
6473
}
6574

6675
public function testBeforeOrEqualSpecificDateRule()
6776
{
6877
$rule = Rule::date()->beforeOrEqual(Carbon::parse('2024-01-01'));
6978
$this->assertEquals('date|before_or_equal:2024-01-01', (string) $rule);
79+
80+
$rule = Rule::date()->format('d/m/Y')->beforeOrEqual(Carbon::parse('2024-01-01'));
81+
$this->assertEquals('date_format:d/m/Y|before_or_equal:01/01/2024', (string) $rule);
7082
}
7183

7284
public function testBetweenDatesRule()
7385
{
7486
$rule = Rule::date()->between(Carbon::parse('2024-01-01'), Carbon::parse('2024-02-01'));
7587
$this->assertEquals('date|after:2024-01-01|before:2024-02-01', (string) $rule);
88+
89+
$rule = Rule::date()->format('d/m/Y')->between(Carbon::parse('2024-01-01'), Carbon::parse('2024-02-01'));
90+
$this->assertEquals('date_format:d/m/Y|after:01/01/2024|before:01/02/2024', (string) $rule);
7691
}
7792

7893
public function testBetweenOrEqualDatesRule()
@@ -87,7 +102,7 @@ public function testChainedRules()
87102
->format('Y-m-d')
88103
->after('2024-01-01 00:00:00')
89104
->before('2025-01-01 00:00:00');
90-
$this->assertEquals('date|date_format:Y-m-d|after:2024-01-01 00:00:00|before:2025-01-01 00:00:00', (string) $rule);
105+
$this->assertEquals('date_format:Y-m-d|after:2024-01-01 00:00:00|before:2025-01-01 00:00:00', (string) $rule);
91106

92107
$rule = Rule::date()
93108
->format('Y-m-d')
@@ -97,7 +112,7 @@ public function testChainedRules()
97112
->unless(true, function ($rule) {
98113
$rule->before('2025-01-01');
99114
});
100-
$this->assertSame('date|date_format:Y-m-d|after:2024-01-01', (string) $rule);
115+
$this->assertSame('date_format:Y-m-d|after:2024-01-01', (string) $rule);
101116
}
102117

103118
public function testDateValidation()

tests/Validation/ValidationRuleParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,15 @@ public function testExplodeHandlesDateRuleWithAdditionalRules()
398398
]));
399399

400400
$rules = [
401-
'date' => Rule::date()->format('Y-m-d'),
401+
'date' => Rule::date()->after('today'),
402402
];
403403

404404
$results = $parser->explode($rules);
405405

406406
$this->assertEquals([
407407
'date' => [
408408
'date',
409-
'date_format:Y-m-d',
409+
'after:today',
410410
],
411411
], $results->rules);
412412
}

0 commit comments

Comments
 (0)