Skip to content

Commit d0c969b

Browse files
authored
Required if declined rule helper (#43)
* test: Fixed deprecated mismatched param name support `Providing invalid named argument $rule for method Sourcetoad\RuleHelper\Tests\Unit\DatabaseRuleTest::testDatabaseRules() is deprecated and will not be supported in PHPUnit 11.0.` * update: Added `required_if_declined` helper * refactor: Fixed in type * refactor: Fixed not_in type
1 parent 5cd974a commit d0c969b

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v5
44

5+
### Upgrading from v5.0 to v5.1
6+
7+
- Minimum Laravel version increased from `11.0.3` to `11.4`.
8+
59
### Upgrading from v4.3 to v5.0
610

711
- Minimum Laravel version increased from `10.46` to `11.0.3`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.2",
9-
"laravel/framework": "^11.0.3"
9+
"laravel/framework": "^11.4"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,8 @@ public static function requiredIf(mixed $callback): RequiredIf
996996
}
997997

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

1029+
/**
1030+
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
1031+
* false, or "false".
1032+
*
1033+
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
1034+
*/
1035+
public static function requiredIfDeclined(string $field): string
1036+
{
1037+
return 'required_if_declined:'.$field;
1038+
}
1039+
10291040
/**
10301041
* The field under validation must be present and not empty if the *anotherField* field is equal to any *value*.
10311042
*

src/RuleSet.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public function image(): self
645645
* list of values provided to the *in* rule.
646646
*
647647
* @link https://laravel.com/docs/11.x/validation#rule-in
648-
* @param Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string $values
648+
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
649649
*/
650650
public function in(Arrayable|array|string $values): self
651651
{
@@ -888,7 +888,7 @@ public function multipleOf(int|float $value): self
888888
* The field under validation must not be included in the given list of values.
889889
*
890890
* @link https://laravel.com/docs/11.x/validation#rule-not-in
891-
* @param Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|array<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>|string $values
891+
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
892892
*/
893893
public function notIn(Arrayable|array|string $values): self
894894
{
@@ -1088,8 +1088,8 @@ public function requiredIf(mixed $callback): self
10881088
}
10891089

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

1117+
/**
1118+
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
1119+
* false, or "false".
1120+
*
1121+
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
1122+
*/
1123+
public function requiredIfDeclined(string $field): self
1124+
{
1125+
return $this->rule(Rule::requiredIfDeclined($field));
1126+
}
1127+
11171128
/**
11181129
* The field under validation must be present and not empty if the *anotherField* field is equal to any *value*.
11191130
*

tests/Unit/DatabaseRuleTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function databaseSetupProvider(): array
5454
return [
5555
'does not exist without column' => [
5656
'createData' => fn() => ['email' => $this->faker->email],
57-
'rule' => fn() => [
57+
'createRules' => fn() => [
5858
'email' => RuleSet::create()->exists('users'),
5959
],
6060
'fails' => true,
@@ -70,14 +70,14 @@ public static function databaseSetupProvider(): array
7070

7171
return ['email' => $email];
7272
},
73-
'rule' => fn() => [
73+
'createRules' => fn() => [
7474
'email' => RuleSet::create()->exists('users'),
7575
],
7676
'fails' => false,
7777
],
7878
'does not exist with column' => [
7979
'createData' => fn() => ['value' => $this->faker->email],
80-
'rule' => fn() => [
80+
'createRules' => fn() => [
8181
'value' => RuleSet::create()->exists('users', 'email'),
8282
],
8383
'fails' => true,
@@ -93,7 +93,7 @@ public static function databaseSetupProvider(): array
9393

9494
return ['value' => $email];
9595
},
96-
'rule' => fn() => [
96+
'createRules' => fn() => [
9797
'value' => RuleSet::create()->exists('users', 'email'),
9898
],
9999
'fails' => false,
@@ -109,7 +109,7 @@ public static function databaseSetupProvider(): array
109109

110110
return ['value' => $email];
111111
},
112-
'rule' => fn() => [
112+
'createRules' => fn() => [
113113
'value' => RuleSet::create()->exists(
114114
'users',
115115
'email',
@@ -129,7 +129,7 @@ public static function databaseSetupProvider(): array
129129

130130
return ['value' => $email];
131131
},
132-
'rule' => fn() => [
132+
'createRules' => fn() => [
133133
'value' => RuleSet::create()->exists(
134134
'users',
135135
'email',
@@ -149,14 +149,14 @@ public static function databaseSetupProvider(): array
149149

150150
return ['email' => $email];
151151
},
152-
'rule' => fn() => [
152+
'createRules' => fn() => [
153153
'email' => RuleSet::create()->unique('users'),
154154
],
155155
'fails' => true,
156156
],
157157
'unique without column' => [
158158
'createData' => fn() => ['email' => $this->faker->email],
159-
'rule' => fn() => [
159+
'createRules' => fn() => [
160160
'email' => RuleSet::create()->unique('users'),
161161
],
162162
'fails' => false,
@@ -172,14 +172,14 @@ public static function databaseSetupProvider(): array
172172

173173
return ['value' => $email];
174174
},
175-
'rule' => fn() => [
175+
'createRules' => fn() => [
176176
'value' => RuleSet::create()->unique('users', 'email'),
177177
],
178178
'fails' => true,
179179
],
180180
'unique with column' => [
181181
'createData' => fn() => ['value' => $this->faker->email],
182-
'rule' => fn() => [
182+
'createRules' => fn() => [
183183
'value' => RuleSet::create()->unique('users', 'email'),
184184
],
185185
'fails' => false,
@@ -195,7 +195,7 @@ public static function databaseSetupProvider(): array
195195

196196
return ['value' => $email];
197197
},
198-
'rule' => fn() => [
198+
'createRules' => fn() => [
199199
'value' => RuleSet::create()->unique(
200200
'users',
201201
'email',
@@ -215,7 +215,7 @@ public static function databaseSetupProvider(): array
215215

216216
return ['value' => $email];
217217
},
218-
'rule' => fn() => [
218+
'createRules' => fn() => [
219219
'value' => RuleSet::create()->unique(
220220
'users',
221221
'email',

tests/Unit/RuleTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,26 @@ public static function ruleDataProvider(): array
21102110
],
21112111
'fails' => true,
21122112
],
2113+
'requiredIfDeclined valid' => [
2114+
'data' => [
2115+
'field-a' => 'a',
2116+
'field-b' => '1',
2117+
],
2118+
'rules' => fn() => [
2119+
'field-a' => RuleSet::create()->requiredIfDeclined('field-b'),
2120+
],
2121+
'fails' => false,
2122+
],
2123+
'requiredIfDeclined invalid' => [
2124+
'data' => [
2125+
'field-a' => '',
2126+
'field-b' => '0',
2127+
],
2128+
'rules' => fn() => [
2129+
'field-a' => RuleSet::create()->requiredIfDeclined('field-b'),
2130+
],
2131+
'fails' => true,
2132+
],
21132133
'requiredIfValue valid' => [
21142134
'data' => [
21152135
'field-a' => 'a',

0 commit comments

Comments
 (0)