Skip to content

Commit 31f8157

Browse files
committed
Add required_without_all rule
1 parent 581e7ca commit 31f8157

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/Rules/RequiredWithoutAll.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Rules;
4+
5+
use Rakit\Validation\Rule;
6+
7+
class RequiredWithoutAll extends Required
8+
{
9+
protected $implicit = true;
10+
11+
protected $message = "The :attribute is required";
12+
13+
public function fillParameters(array $params)
14+
{
15+
$this->params['fields'] = $params;
16+
return $this;
17+
}
18+
19+
public function check($value)
20+
{
21+
$this->requireParameters(['fields']);
22+
$fields = $this->parameter('fields');
23+
$validator = $this->validation->getValidator();
24+
$required_validator = $validator('required');
25+
26+
foreach($fields as $field) {
27+
if ($this->validation->hasValue($field)) {
28+
return true;
29+
}
30+
}
31+
32+
$this->setAttributeAsRequired();
33+
return $required_validator->check($value, []);
34+
}
35+
36+
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function registerBaseValidators()
7979
'required_with' => new Rules\RequiredWith,
8080
'required_without' => new Rules\RequiredWithout,
8181
'required_with_all' => new Rules\RequiredWithAll,
82+
'required_without_all' => new Rules\RequiredWithoutAll,
8283
'email' => new Rules\Email,
8384
'alpha' => new Rules\Alpha,
8485
'numeric' => new Rules\Numeric,

tests/ValidatorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ public function testRequiredWithAllRule()
241241
$this->assertFalse($v2->passes());
242242
}
243243

244+
public function testRequiredWithoutAllRule()
245+
{
246+
$v1 = $this->validator->validate([
247+
'b' => '',
248+
'a' => '1'
249+
], [
250+
'b' => 'required_without_all:a,c'
251+
]);
252+
253+
$this->assertTrue($v1->passes());
254+
255+
$v2 = $this->validator->validate([
256+
'b' => '',
257+
], [
258+
'b' => 'required_without_all:a,c'
259+
]);
260+
261+
$this->assertFalse($v2->passes());
262+
}
263+
244264
public function testRulePresent()
245265
{
246266
$v1 = $this->validator->validate([

0 commit comments

Comments
 (0)