diff --git a/src/Attributes/Validation/DateFormat.php b/src/Attributes/Validation/DateFormat.php index e6228774..2886a1fc 100644 --- a/src/Attributes/Validation/DateFormat.php +++ b/src/Attributes/Validation/DateFormat.php @@ -3,12 +3,17 @@ namespace Spatie\LaravelData\Attributes\Validation; use Attribute; +use Illuminate\Support\Arr; +use Spatie\LaravelData\Support\Validation\References\RouteParameterReference; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] class DateFormat extends StringValidationAttribute { - public function __construct(protected string $format) + protected string|array $format; + + public function __construct(string|array|RouteParameterReference ...$format) { + $this->format = Arr::flatten($format); } public static function keyword(): string diff --git a/tests/Datasets/RulesDataset.php b/tests/Datasets/RulesDataset.php index fdc878dd..1bcff5d3 100644 --- a/tests/Datasets/RulesDataset.php +++ b/tests/Datasets/RulesDataset.php @@ -124,6 +124,7 @@ function fixature( yield from betweenAttributes(); yield from currentPasswordAttributes(); yield from dateEqualsAttributes(); + yield from dateFormatAttributes(); yield from dimensionsAttributes(); yield from distinctAttributes(); yield from doesntEndWithAttributes(); @@ -195,11 +196,6 @@ function fixature( expected: 'date', ); - yield fixature( - attribute: new DateFormat('Y-m-d'), - expected: 'date_format:Y-m-d', - ); - yield fixature( attribute: new Declined(), expected: 'declined', @@ -584,6 +580,24 @@ function dateEqualsAttributes(): Generator ); } +function dateFormatAttributes(): Generator +{ + yield fixature( + attribute: new DateFormat('Y-m-d'), + expected: 'date_format:Y-m-d', + ); + + yield fixature( + attribute: new DateFormat(['Y-m-d', 'Y-m-d H:i:s']), + expected: 'date_format:Y-m-d,Y-m-d H:i:s', + ); + + yield fixature( + attribute: new DateFormat('Y-m-d', 'Y-m-d H:i:s'), + expected: 'date_format:Y-m-d,Y-m-d H:i:s', + ); +} + function dimensionsAttributes(): Generator { yield fixature(