Skip to content

Commit

Permalink
Fix #302
Browse files Browse the repository at this point in the history
  • Loading branch information
filisko committed Mar 9, 2020
1 parent ce371c2 commit 924e218
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
18 changes: 10 additions & 8 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,17 @@ public function validate(array $input, array $ruleset, string $rules_delimiter='
{
$this->errors = [];

foreach ($ruleset as $field => $rules) {
foreach ($ruleset as $field => $rawRules) {
$input[$field] = $input[$field] ?? null;

$rules = $this->parse_rules($rules, $rules_delimiter);
$require_rule_found = $this->find_required_rule($rules);
$rules = $this->parse_rules($rawRules, $rules_delimiter);
$is_required = $this->field_has_required_rules($rules);

foreach ($rules as $rule) {
if (is_null($require_rule_found) && self::is_empty($input[$field])) continue;
if (!$is_required && self::is_empty($input[$field])) {
continue;
}

foreach ($rules as $rule) {
$parsed_rule = $this->parse_rule($rule, $parameters_delimiter);
$result = $this->call_rule($parsed_rule['rule'], $field, $input, $parsed_rule['param']);

Expand Down Expand Up @@ -486,7 +488,7 @@ private function parse_rule_param($param)
return $param;
}

private function find_required_rule(array $rules)
private function field_has_required_rules(array $rules)
{
$require_type_of_rules = ['required', 'required_file'];

Expand All @@ -495,11 +497,11 @@ private function find_required_rule(array $rules)
$found = array_filter($rules, function($item) use($require_type_of_rules) {
return in_array($item[0], $require_type_of_rules);
});
return $found > 0 ? true : false;
return count($found) > 0;
}

$found = array_values(array_intersect($require_type_of_rules, $rules));
return count($found) > 0 ? $found[0] : null;
return count($found) > 0;
}

private static function validator_to_method(string $rule)
Expand Down
33 changes: 33 additions & 0 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,39 @@ public function testRulesArrayFormatWithOneParameter()
]], $result);
}


public function testRulesArrayFormatIgnoresNonRequiredFields()
{
$result = $this->gump->validate([
'some_other_field' => null
], [
'some_field' => ['boolean', 'min_len' => 2],
'some_other_field' => ['integer', 'min_len' => 2],
]);

$this->assertTrue($result);
}

public function testRulesArrayFormatChecksRequiredFields()
{
$result = $this->gump->validate([], [
'some_field' => ['required', 'boolean', 'min_len' => 2],
'some_other_field' => ['required', 'integer', 'min_len' => 2],
]);

$this->assertEquals($result, [[
'field' => 'some_field',
'value' => null,
'rule' => 'validate_required',
'param' => null
], [
'field' => 'some_other_field',
'value' => null,
'rule' => 'validate_required',
'param' => null
]]);
}

public function testRulesArrayFormatWithSimpleArrayParameters()
{
$result = $this->gump->validate([
Expand Down

0 comments on commit 924e218

Please sign in to comment.