Skip to content

Commit d63d0eb

Browse files
committed
feat(validation): add date default to validated input
- Allow date() to return a Time default for missing fields - Keep explicit null and invalid present values behavior unchanged - Document strict ValidatedInput behavior and thrown exceptions - Cover date defaults in tests and user guide Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 2f5d1bd commit d63d0eb

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

system/Validation/ValidatedInput.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
use UnitEnum;
2323

2424
/**
25+
* Represents validated input data.
26+
*
27+
* This class is stricter than InputData: missing values may use defaults and
28+
* null values remain null, but invalid present values throw.
29+
*
2530
* @see \CodeIgniter\Validation\ValidatedInputTest
2631
*/
2732
class ValidatedInput extends InputData
@@ -30,12 +35,19 @@ class ValidatedInput extends InputData
3035
* Returns a validated field as a Time instance.
3136
*
3237
* Supports dot-array syntax for nested validated data.
38+
*
39+
* @throws InvalidArgumentException
3340
*/
3441
public function date(
3542
string $key,
3643
?string $format = null,
3744
DateTimeZone|string|null $timezone = null,
45+
?Time $default = null,
3846
): ?Time {
47+
if (! $this->has($key)) {
48+
return $default;
49+
}
50+
3951
$value = $this->get($key);
4052

4153
if ($value === null) {
@@ -68,6 +80,8 @@ public function date(
6880
* @param TEnum|null $default
6981
*
7082
* @return TEnum|null
83+
*
84+
* @throws InvalidArgumentException
7185
*/
7286
public function enum(string $key, string $enumClass, ?UnitEnum $default = null): ?UnitEnum
7387
{

tests/system/Validation/ValidatedInputTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function testDateSupportsCustomFormat(): void
5151
$this->assertSame('2026-05-04', $input->date('published_at', 'd/m/Y')->toDateString());
5252
}
5353

54+
public function testDateReturnsDefaultForMissingValidatedField(): void
55+
{
56+
$input = new ValidatedInput([]);
57+
$default = Time::parse('2026-05-04');
58+
59+
$this->assertSame($default, $input->date('published_at', default: $default));
60+
}
61+
5462
public function testDateThrowsForInvalidValidatedValue(): void
5563
{
5664
$input = new ValidatedInput(['published_at' => '']);
@@ -165,8 +173,9 @@ public function testTypedAccessorsReturnNullForNullValidatedFields(): void
165173
'published_at' => null,
166174
'status' => null,
167175
]);
176+
$default = Time::parse('2026-05-04');
168177

169-
$this->assertNotInstanceOf(Time::class, $input->date('published_at'));
178+
$this->assertNull($input->date('published_at', default: $default));
170179
$this->assertNotInstanceOf(StatusEnum::class, $input->enum('status', StatusEnum::class, StatusEnum::PENDING));
171180
}
172181
}

user_guide_src/source/libraries/validation.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,10 @@ All methods support dot-array syntax for nested validated data.
521521
missing, it returns the default value or ``null``.
522522
* ``array($key, $default = null)`` returns ``array|null``. If the field is
523523
missing, it returns the default value or ``null``.
524-
* ``date($key, $format = null, $timezone = null)`` returns
525-
:php:class:`CodeIgniter\\I18n\\Time` or ``null``. Pass a format when the value
526-
should be parsed with a specific date format.
524+
* ``date($key, $format = null, $timezone = null, $default = null)`` returns
525+
:php:class:`CodeIgniter\\I18n\\Time` or ``null``. If the field is missing, it
526+
returns the default value or ``null``. Pass a format when the value should be
527+
parsed with a specific date format.
527528
* ``enum($key, $enumClass, $default = null)`` returns an enum instance or
528529
``null``. The default value must be ``null`` or an instance of the requested
529530
enum class.

0 commit comments

Comments
 (0)