Skip to content

Commit

Permalink
update: allowed null for required unless
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 11, 2024
1 parent 314374f commit 98dc925
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,12 +1108,16 @@ public static function requiredIfValue(string $anotherField, string ...$value):

/**
* The field under validation must be present and not empty unless the *anotherField* field is equal to any
* *value*.
* *value*. This also means *anotherField* must be present in the request data unless value is *null*. If value is
* *null*, the field under validation will be required unless the comparison field is null or the comparison field
* is missing from the request data.
*
* @link https://laravel.com/docs/11.x/validation#rule-required-unless
*/
public static function requiredUnless(string $anotherField, string ...$value): string
public static function requiredUnless(string $anotherField, ?string ...$value): string
{
$value = array_map(fn(?string $value) => $value ?? 'null', $value);

return sprintf('required_unless:%s,%s', $anotherField, implode(',', $value));
}

Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,11 +1178,13 @@ public function requiredIfValue(string $anotherField, string ...$value): self

/**
* The field under validation must be present and not empty unless the *anotherField* field is equal to any
* *value*.
* *value*. This also means *anotherField* must be present in the request data unless value is *null*. If value is
* *null*, the field under validation will be required unless the comparison field is null or the comparison field
* is missing from the request data.
*
* @link https://laravel.com/docs/11.x/validation#rule-required-unless
*/
public function requiredUnless(string $anotherField, string ...$value): self
public function requiredUnless(string $anotherField, ?string ...$value): self
{
return $this->rule(Rule::requiredUnless($anotherField, ...$value));
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,35 @@ public static function ruleDataProvider(): array
],
'fails' => true,
],
'requiredUnless null valid missing' => [
'data' => [
'field-a' => '',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->requiredUnless('field-b', null),
],
'fails' => false,
],
'requiredUnless null valid' => [
'data' => [
'field-a' => '',
'field-b' => null,
],
'rules' => fn() => [
'field-a' => RuleSet::create()->requiredUnless('field-b', null),
],
'fails' => false,
],
'requiredUnless null invalid' => [
'data' => [
'field-a' => '',
'field-b' => 'd',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->requiredUnless('field-b', null),
],
'fails' => true,
],
'requiredWith valid - required' => [
'data' => [
'field-a' => 'a',
Expand Down

0 comments on commit 98dc925

Please sign in to comment.