Skip to content

Commit 7dcbbf9

Browse files
authored
Prohibited if update (#53)
* update: add prohibited_if_accepted and prohibited_if_declined * update: updated minimum version
1 parent 7dea152 commit 7dcbbf9

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
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.5 to v5.6
6+
7+
- Minimum Laravel version increased from `11.23.2` to `11.43`.
8+
59
### Upgrading from v5.2 to v5.3
610

711
- Minimum Laravel version increased from `11.8` to `11.23.2`.

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||^8.3||^8.4",
9-
"laravel/framework": "^11.23.2"
9+
"laravel/framework": "^11.43"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,24 @@ public static function prohibitedIf(mixed $callback): ProhibitedIf
10431043
return LaravelRule::prohibitedIf($callback);
10441044
}
10451045

1046+
/**
1047+
* The field under validation must be empty or not present in the input data if the *anotherField* field
1048+
* is equal to "yes", "on", 1, "1", true, or "true".
1049+
*/
1050+
public static function prohibitedIfAccepted(string $anotherField): string
1051+
{
1052+
return sprintf('prohibited_if_accepted:%s', $anotherField);
1053+
}
1054+
1055+
/**
1056+
* The field under validation must be empty or not present in the input data if the *anotherField* field
1057+
* is equal to "no", "off", 0, "0", false, or "false".
1058+
*/
1059+
public static function prohibitedIfDeclined(string $anotherField): string
1060+
{
1061+
return sprintf('prohibited_if_declined:%s', $anotherField);
1062+
}
1063+
10461064
/**
10471065
* The field under validation must be empty or not present if the *anotherField* field is equal to any *value*.
10481066
* - The value is *null*.

src/RuleSet.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,24 @@ public function prohibitedIf(mixed $callback): self
11201120
return $this->rule(Rule::prohibitedIf($callback));
11211121
}
11221122

1123+
/**
1124+
* The field under validation must be empty or not present in the input data if the *anotherField* field
1125+
* is equal to "yes", "on", 1, "1", true, or "true".
1126+
*/
1127+
public function prohibitedIfAccepted(string $anotherField): self
1128+
{
1129+
return $this->rule(Rule::prohibitedIfAccepted($anotherField));
1130+
}
1131+
1132+
/**
1133+
* The field under validation must be empty or not present in the input data if the *anotherField* field
1134+
* is equal to "no", "off", 0, "0", false, or "false".
1135+
*/
1136+
public function prohibitedIfDeclined(string $anotherField): self
1137+
{
1138+
return $this->rule(Rule::prohibitedIfDeclined($anotherField));
1139+
}
1140+
11231141
/**
11241142
* The field under validation must be empty or not present if the *anotherField* field is equal to any *value*.
11251143
* - The value is *null*.

tests/Unit/RuleTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,126 @@ public static function ruleDataProvider(): array
21782178
],
21792179
'fails' => true,
21802180
],
2181+
'prohibitedIfAccepted valid number' => [
2182+
'data' => [
2183+
'field-a' => '1',
2184+
'field-b' => 0,
2185+
],
2186+
'rules' => fn() => [
2187+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2188+
],
2189+
'fails' => false,
2190+
],
2191+
'prohibitedIfAccepted valid boolean' => [
2192+
'data' => [
2193+
'field-a' => '1',
2194+
'field-b' => false,
2195+
],
2196+
'rules' => fn() => [
2197+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2198+
],
2199+
'fails' => false,
2200+
],
2201+
'prohibitedIfAccepted valid string' => [
2202+
'data' => [
2203+
'field-a' => '1',
2204+
'field-b' => 'off',
2205+
],
2206+
'rules' => fn() => [
2207+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2208+
],
2209+
'fails' => false,
2210+
],
2211+
'prohibitedIfAccepted invalid number' => [
2212+
'data' => [
2213+
'field-a' => '1',
2214+
'field-b' => 1,
2215+
],
2216+
'rules' => fn() => [
2217+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2218+
],
2219+
'fails' => true,
2220+
],
2221+
'prohibitedIfAccepted invalid boolean' => [
2222+
'data' => [
2223+
'field-a' => '1',
2224+
'field-b' => true,
2225+
],
2226+
'rules' => fn() => [
2227+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2228+
],
2229+
'fails' => true,
2230+
],
2231+
'prohibitedIfAccepted invalid string' => [
2232+
'data' => [
2233+
'field-a' => '1',
2234+
'field-b' => 'on',
2235+
],
2236+
'rules' => fn() => [
2237+
'field-a' => RuleSet::create()->prohibitedIfAccepted('field-b'),
2238+
],
2239+
'fails' => true,
2240+
],
2241+
'prohibitedIfDeclined valid number' => [
2242+
'data' => [
2243+
'field-a' => '1',
2244+
'field-b' => 1,
2245+
],
2246+
'rules' => fn() => [
2247+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2248+
],
2249+
'fails' => false,
2250+
],
2251+
'prohibitedIfDeclined valid boolean' => [
2252+
'data' => [
2253+
'field-a' => '1',
2254+
'field-b' => true,
2255+
],
2256+
'rules' => fn() => [
2257+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2258+
],
2259+
'fails' => false,
2260+
],
2261+
'prohibitedIfDeclined valid string' => [
2262+
'data' => [
2263+
'field-a' => '1',
2264+
'field-b' => 'on',
2265+
],
2266+
'rules' => fn() => [
2267+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2268+
],
2269+
'fails' => false,
2270+
],
2271+
'prohibitedIfDeclined invalid number' => [
2272+
'data' => [
2273+
'field-a' => '1',
2274+
'field-b' => 0,
2275+
],
2276+
'rules' => fn() => [
2277+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2278+
],
2279+
'fails' => true,
2280+
],
2281+
'prohibitedIfDeclined invalid boolean' => [
2282+
'data' => [
2283+
'field-a' => '1',
2284+
'field-b' => false,
2285+
],
2286+
'rules' => fn() => [
2287+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2288+
],
2289+
'fails' => true,
2290+
],
2291+
'prohibitedIfDeclined invalid string' => [
2292+
'data' => [
2293+
'field-a' => '1',
2294+
'field-b' => 'off',
2295+
],
2296+
'rules' => fn() => [
2297+
'field-a' => RuleSet::create()->prohibitedIfDeclined('field-b'),
2298+
],
2299+
'fails' => true,
2300+
],
21812301
'prohibitedIfValue valid' => [
21822302
'data' => [
21832303
'field-a' => '',

0 commit comments

Comments
 (0)