Skip to content

Commit dd29b05

Browse files
committed
feat: add field_exists rule
1 parent 3c5adf2 commit dd29b05

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

system/Validation/Rules.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,22 @@ public function required_without(
425425

426426
return true;
427427
}
428+
429+
/**
430+
* The field exists in $data.
431+
*
432+
* @param array|bool|float|int|object|string|null $value The field value.
433+
* @param string|null $param The rule's parameter.
434+
* @param array $data The data to be validated.
435+
* @param string|null $field The field name.
436+
*/
437+
public function field_exists(
438+
$value = null,
439+
?string $param = null,
440+
array $data = [],
441+
?string $error = null,
442+
?string $field = null
443+
): bool {
444+
return (bool) (array_key_exists($field, $data));
445+
}
428446
}

system/Validation/StrictRules/Rules.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,22 @@ public function required_without(
373373
): bool {
374374
return $this->nonStrictRules->required_without($str, $otherFields, $data, $error, $field);
375375
}
376+
377+
/**
378+
* The field exists in $data.
379+
*
380+
* @param array|bool|float|int|object|string|null $value The field value.
381+
* @param string|null $param The rule's parameter.
382+
* @param array $data The data to be validated.
383+
* @param string|null $field The field name.
384+
*/
385+
public function field_exists(
386+
$value = null,
387+
?string $param = null,
388+
array $data = [],
389+
?string $error = null,
390+
?string $field = null
391+
): bool {
392+
return (bool) (array_key_exists($field, $data));
393+
}
376394
}

system/Validation/Validation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,9 @@ protected function processRules(
323323
continue;
324324
}
325325

326-
$found = true;
327-
$passed = $param === false
326+
$found = true;
327+
328+
$passed = ($param === false && $rule !== 'field_exists')
328329
? $set->{$rule}($value, $error)
329330
: $set->{$rule}($value, $param, $data, $error, $field);
330331

tests/system/Validation/RulesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,44 @@ public static function provideRequiredWithoutMultipleWithoutFields(): iterable
830830
],
831831
];
832832
}
833+
834+
/**
835+
* @dataProvider provideFieldExists
836+
*/
837+
public function testFieldExists(array $rules, array $data, bool $expected): void
838+
{
839+
$this->validation->setRules($rules);
840+
$this->assertSame($expected, $this->validation->run($data));
841+
}
842+
843+
public static function provideFieldExists(): iterable
844+
{
845+
yield from [
846+
[
847+
['foo' => 'field_exists'],
848+
['foo' => ''],
849+
true,
850+
],
851+
[
852+
['foo' => 'field_exists'],
853+
['foo' => null],
854+
true,
855+
],
856+
[
857+
['foo' => 'field_exists'],
858+
['foo' => false],
859+
true,
860+
],
861+
[
862+
['foo' => 'field_exists'],
863+
['foo' => []],
864+
true,
865+
],
866+
[
867+
['foo' => 'field_exists'],
868+
[],
869+
false,
870+
],
871+
];
872+
}
833873
}

0 commit comments

Comments
 (0)