Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required if declined rule helper #43

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 15 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public function image(): self
* list of values provided to the *in* rule.
*
* @link https://laravel.com/docs/11.x/validation#rule-in
* @param Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string $values
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
*/
public function in(Arrayable|array|string $values): self
{
Expand Down Expand Up @@ -888,7 +888,7 @@ public function multipleOf(int|float $value): self
* The field under validation must not be included in the given list of values.
*
* @link https://laravel.com/docs/11.x/validation#rule-not-in
* @param Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string $values
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
*/
public function notIn(Arrayable|array|string $values): self
{
Expand Down 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
24 changes: 12 additions & 12 deletions tests/Unit/DatabaseRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function databaseSetupProvider(): array
return [
'does not exist without column' => [
'createData' => fn() => ['email' => $this->faker->email],
'rule' => fn() => [
'createRules' => fn() => [
'email' => RuleSet::create()->exists('users'),
],
'fails' => true,
Expand All @@ -70,14 +70,14 @@ public static function databaseSetupProvider(): array

return ['email' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'email' => RuleSet::create()->exists('users'),
],
'fails' => false,
],
'does not exist with column' => [
'createData' => fn() => ['value' => $this->faker->email],
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->exists('users', 'email'),
],
'fails' => true,
Expand All @@ -93,7 +93,7 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->exists('users', 'email'),
],
'fails' => false,
Expand All @@ -109,7 +109,7 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->exists(
'users',
'email',
Expand All @@ -129,7 +129,7 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->exists(
'users',
'email',
Expand All @@ -149,14 +149,14 @@ public static function databaseSetupProvider(): array

return ['email' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'email' => RuleSet::create()->unique('users'),
],
'fails' => true,
],
'unique without column' => [
'createData' => fn() => ['email' => $this->faker->email],
'rule' => fn() => [
'createRules' => fn() => [
'email' => RuleSet::create()->unique('users'),
],
'fails' => false,
Expand All @@ -172,14 +172,14 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->unique('users', 'email'),
],
'fails' => true,
],
'unique with column' => [
'createData' => fn() => ['value' => $this->faker->email],
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->unique('users', 'email'),
],
'fails' => false,
Expand All @@ -195,7 +195,7 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->unique(
'users',
'email',
Expand All @@ -215,7 +215,7 @@ public static function databaseSetupProvider(): array

return ['value' => $email];
},
'rule' => fn() => [
'createRules' => fn() => [
'value' => RuleSet::create()->unique(
'users',
'email',
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