Skip to content

Commit

Permalink
update: Added required_if_declined helper
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Apr 16, 2024
1 parent ef31dd0 commit 9bb4f62
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v5

### Upgrading from v5.0 to v5.1

- Minimum Laravel version increased from `11.0.3` to `11.4`.

### Upgrading from v4.3 to v5.0

- Minimum Laravel version increased from `10.46` to `11.0.3`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.2",
"laravel/framework": "^11.0.3"
"laravel/framework": "^11.4"
},
"require-dev": {
"ext-json": "*",
Expand Down
15 changes: 13 additions & 2 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,8 @@ public static function requiredIf(mixed $callback): RequiredIf
}

/**
* The field under validation must be present and not empty if the `anotherfield` field is equal to yes, on, 1, "1",
* true, or "true".
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
* or "true".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
*/
Expand Down Expand Up @@ -1026,6 +1026,17 @@ public static function requiredIfAny(RequiredIf ...$rules): RequiredIf
});
}

/**
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
* false, or "false".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
*/
public static function requiredIfDeclined(string $field): string
{
return 'required_if_declined:'.$field;
}

/**
* The field under validation must be present and not empty if the *anotherField* field is equal to any *value*.
*
Expand Down
15 changes: 13 additions & 2 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,8 @@ public function requiredIf(mixed $callback): self
}

/**
* The field under validation must be present and not empty if the `anotherfield` field is equal to yes, on, 1, "1",
* true, or "true".
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
* or "true".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
*/
Expand All @@ -1114,6 +1114,17 @@ public function requiredIfAny(RequiredIf ...$rules): self
return $this->rule(Rule::requiredIfAny(...$rules));
}

/**
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
* false, or "false".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
*/
public function requiredIfDeclined(string $field): self
{
return $this->rule(Rule::requiredIfDeclined($field));
}

/**
* The field under validation must be present and not empty if the *anotherField* field is equal to any *value*.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,26 @@ public static function ruleDataProvider(): array
],
'fails' => true,
],
'requiredIfDeclined valid' => [
'data' => [
'field-a' => 'a',
'field-b' => '1',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->requiredIfDeclined('field-b'),
],
'fails' => false,
],
'requiredIfDeclined invalid' => [
'data' => [
'field-a' => '',
'field-b' => '0',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->requiredIfDeclined('field-b'),
],
'fails' => true,
],
'requiredIfValue valid' => [
'data' => [
'field-a' => 'a',
Expand Down

0 comments on commit 9bb4f62

Please sign in to comment.