Skip to content

Commit 2e5ba48

Browse files
authored
Merge pull request #335 from Wixel/fix-315
Fixes issue #315
2 parents 040be7c + b638593 commit 2e5ba48

8 files changed

+38
-16
lines changed

gump.class.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private function field_has_required_rules(array $rules)
537537
{
538538
$require_type_of_rules = ['required', 'required_file'];
539539

540-
// v2
540+
// v2 format (using arrays for definition of rules)
541541
if (is_array($rules) && is_array($rules[0])) {
542542
$found = array_filter($rules, function ($item) use ($require_type_of_rules) {
543543
return in_array($item[0], $require_type_of_rules);
@@ -583,10 +583,20 @@ private static function filter_to_method(string $rule)
583583
*/
584584
private function foreach_call_validator(string $rule, string $field, array $input, array $rule_params = [])
585585
{
586-
$values = !is_array($input[$field]) ? [ $input[$field] ] : $input[$field];
586+
$is_required_kind_of_rule = $this->field_has_required_rules([$rule]);
587+
588+
// Fixes #315
589+
if ($is_required_kind_of_rule && is_array($input[$field]) && count($input[$field]) === 0) {
590+
$result = $this->call_validator($rule, $field, $input, $rule_params, $input[$field]);
591+
592+
return is_array($result) ? $result : true;
593+
}
594+
595+
$values = is_array($input[$field]) ? $input[$field] : [ $input[$field] ];
587596

588597
foreach ($values as $value) {
589598
$result = $this->call_validator($rule, $field, $input, $rule_params, $value);
599+
590600
if (is_array($result)) {
591601
return $result;
592602
}

tests/RunTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ public function testNestedArrays()
132132
'field2.*.name' => 'The Field2.*.name field is required'
133133
], $this->gump->get_errors_array());
134134
}
135-
}
135+
}

tests/Validators/MaxNumericValidatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
3232
{
3333
$this->assertTrue($this->validate('max_numeric,2', ''));
3434
}
35-
}
35+
}

tests/Validators/MinAgeValidatorTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class MinAgeValidatorTest extends BaseTestCase
1717
{
1818
public function testSameDayAsBirthdayIsSuccess()
1919
{
20-
21-
2220
$this->helpersMock->shouldReceive('date')
2321
->once()
2422
->with('Y-m-d', 866419200)
@@ -41,8 +39,6 @@ public function testSameDayAsBirthdayIsSuccess()
4139

4240
public function testOneDayAfterBirthdayIsSuccess()
4341
{
44-
45-
4642
$this->helpersMock->shouldReceive('date')
4743
->once()
4844
->with('Y-m-d', 866419200)
@@ -107,4 +103,4 @@ public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
107103

108104
$this->assertTrue($result);
109105
}
110-
}
106+
}

tests/Validators/RequiredValidatorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function errorProvider()
4646
return [
4747
[''],
4848
[null],
49+
[[]],
4950
];
5051
}
5152

@@ -87,4 +88,4 @@ public function testRequiredValidatorsReturnRightErrorStructureWhenFieldIsPresen
8788
'params' => []
8889
]], $result);
8990
}
90-
}
91+
}

tests/Validators/ValidArraySizeEqualValidatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public function testWhenLesserIsFailure()
3232

3333
public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
3434
{
35-
$this->assertTrue($this->validate(self::RULE, ''));
35+
$this->assertTrue($this->validate(self::RULE, []));
3636
}
37-
}
37+
}

tests/Validators/ValidArraySizeGreaterValidatorTest.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ public function testWhenLesserIsFailure()
3232

3333
public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
3434
{
35-
$this->assertTrue($this->validate(self::RULE, ''));
35+
$this->assertTrue($this->validate(self::RULE, []));
3636
}
37-
}
37+
38+
public function testWhenInputIsEmptyAndRequiredIsError()
39+
{
40+
$this->assertEquals(
41+
$this->validate('required|'.self::RULE, []),
42+
[
43+
[
44+
'field' => 'test',
45+
'value' => [],
46+
'rule' => 'required',
47+
'params' => []
48+
]
49+
]
50+
);
51+
}
52+
}

tests/Validators/ValidArraySizeLesserValidatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public function testWhenGreaterIsFailure()
3232

3333
public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
3434
{
35-
$this->assertTrue($this->validate(self::RULE, ''));
35+
$this->assertTrue($this->validate(self::RULE, []));
3636
}
37-
}
37+
}

0 commit comments

Comments
 (0)