diff --git a/composer.json b/composer.json index d21b9a0..a657257 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "minimum-stability": "stable", "require": { "php": "^8.1", - "laravel/framework": "^10.14" + "laravel/framework": "^10.32" }, "require-dev": { "ext-json": "*", diff --git a/src/Rule.php b/src/Rule.php index dc5677c..a4050c7 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -826,6 +826,42 @@ public static function present(): string return 'present'; } + /** + * The field under validation must be present but can be empty if *anotherField* under validation is equal to a + * specified value. + */ + public static function presentIf(string $anotherField, string ...$value): string + { + return sprintf('present_if:%s,%s', $anotherField, implode(',', $value)); + } + + /** + * The field under validation must be present but can be empty unless the *anotherField* field is equal to any + * *value*. + */ + public static function presentUnless(string $anotherField, string ...$value): string + { + return sprintf('present_unless:%s,%s', $anotherField, implode(',', $value)); + } + + /** + * The field under validation must be present but can be empty *only if* any of the other specified fields are + * present and not empty. + */ + public static function presentWith(string ...$field): string + { + return 'present_with:'.implode(',', $field); + } + + /** + * The field under validation must be present but can be empty *only if* all the other specified fields are present + * and not empty. + */ + public static function presentWithAll(string ...$field): string + { + return 'present_with_all:'.implode(',', $field); + } + /** * The field under validation must be empty or not present. * diff --git a/src/RuleSet.php b/src/RuleSet.php index 359fe2d..a056bc5 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -891,6 +891,42 @@ public function present(): self return $this->rule(Rule::present()); } + /** + * The field under validation must be present but can be empty if *anotherField* under validation is equal to a + * specified value. + */ + public function presentIf(string $anotherField, string ...$value): self + { + return $this->rule(Rule::presentIf($anotherField, ...$value)); + } + + /** + * The field under validation must be present but can be empty unless the *anotherField* field is equal to any + * *value*. + */ + public function presentUnless(string $anotherField, string ...$value): self + { + return $this->rule(Rule::presentUnless($anotherField, ...$value)); + } + + /** + * The field under validation must be present but can be empty *only if* any of the other specified fields are + * present and not empty. + */ + public function presentWith(string ...$field): self + { + return $this->rule(Rule::presentWith(...$field)); + } + + /** + * The field under validation must be present but can be empty *only if* all the other specified fields are present + * and not empty. + */ + public function presentWithAll(string ...$field): self + { + return $this->rule(Rule::presentWithAll(...$field)); + } + /** * The field under validation must be empty or not present. * diff --git a/tests/Unit/RuleTest.php b/tests/Unit/RuleTest.php index 187a353..fdae608 100644 --- a/tests/Unit/RuleTest.php +++ b/tests/Unit/RuleTest.php @@ -1722,6 +1722,119 @@ public function ruleDataProvider(): array ], 'fails' => true, ], + 'presentIf valid' => [ + 'data' => [ + 'field-b' => '', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentIf('field-b', 'a'), + ], + 'fails' => false, + ], + 'presentIf valid with value' => [ + 'data' => [ + 'field-a' => '', + 'field-b' => 'a', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentIf('field-b', 'a'), + ], + 'fails' => false, + ], + 'presentIf invalid' => [ + 'data' => [ + 'field-b' => 'a', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentIf('field-b', 'a'), + ], + 'fails' => true, + ], + 'presentUnless valid' => [ + 'data' => [ + 'field-a' => '', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentUnless('field-b', 'a'), + ], + 'fails' => false, + ], + 'presentUnless valid with value' => [ + 'data' => [ + 'field-b' => 'a', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentUnless('field-b', 'a'), + ], + 'fails' => false, + ], + 'presentUnless invalid' => [ + 'data' => [ + 'field-b' => 'b', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentUnless('field-b', 'a'), + ], + 'fails' => true, + ], + 'presentWith valid' => [ + 'data' => [ + 'field-a' => '', + 'field-b' => 'b', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWith('field-b'), + ], + 'fails' => false, + ], + 'presentWith valid without' => [ + 'data' => [ + 'field-c' => 'c', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWith('field-b'), + ], + 'fails' => false, + ], + 'presentWith invalid' => [ + 'data' => [ + 'field-b' => 'b', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWith('field-b'), + ], + 'fails' => true, + ], + 'presentWithAll valid' => [ + 'data' => [ + 'field-a' => '', + 'field-b' => 'b', + 'field-c' => 'c', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'), + ], + 'fails' => false, + ], + 'presentWithAll valid without' => [ + 'data' => [ + 'field-c' => 'c', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'), + ], + 'fails' => false, + ], + 'presentWithAll invalid' => [ + 'data' => [ + 'field-b' => 'b', + 'field-c' => 'c', + ], + 'rules' => fn() => [ + 'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'), + ], + 'fails' => true, + ], 'prohibited valid' => [ 'data' => [ 'field-a' => '',