Skip to content

Commit

Permalink
update: added support for date format with many supported
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 11, 2024
1 parent fb5eec6 commit 314374f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ public static function dateEquals(string|DateTimeInterface $date): string
}

/**
* The field under validation must match the given *format*.
* The field under validation must match one of the given formats. You should use **either** *date* or *dateFormat*
* when validating a field, not both. This validation rule supports all formats supported by PHP's *DateTime* class.
*
* @link https://laravel.com/docs/11.x/validation#rule-date-format
* @link https://www.php.net/manual/en/datetime.format.php
* @param string $dateFormat A format supported by the *DateTime* class
*/
public static function dateFormat(string $dateFormat): string
public static function dateFormat(string ...$dateFormat): string
{
return 'date_format:'.$dateFormat;
return 'date_format:'.implode(',', $dateFormat);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ public function dateEquals(string|DateTimeInterface $date): self
}

/**
* The field under validation must match the given *format*.
* The field under validation must match one of the given formats. You should use **either** *date* or *dateFormat*
* when validating a field, not both. This validation rule supports all formats supported by PHP's *DateTime* class.
*
* @link https://laravel.com/docs/11.x/validation#rule-date-format
* @link https://www.php.net/manual/en/datetime.format.php
* @param string $dateFormat A format supported by the *DateTime* class
*/
public function dateFormat(string $dateFormat): self
public function dateFormat(string ...$dateFormat): self
{
return $this->rule(Rule::dateFormat($dateFormat));
return $this->rule(Rule::dateFormat(...$dateFormat));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,16 @@ public static function ruleDataProvider(): array
'rules' => fn() => RuleSet::create()->dateFormat('d-M-Y'),
'fails' => true,
],
'dateFormat of many valid' => [
'data' => '2021-Jan-01',
'rules' => fn() => RuleSet::create()->dateFormat('d-M-Y', 'Y-M-d'),
'fails' => false,
],
'dateFormat of many invalid' => [
'data' => '2021-01-01',
'rules' => fn() => RuleSet::create()->dateFormat('d-M-Y', 'Y-M-d'),
'fails' => true,
],
'decimal valid' => [
'data' => '1.1',
'rules' => fn() => RuleSet::create()->decimal(1),
Expand Down

0 comments on commit 314374f

Please sign in to comment.