Skip to content

Commit b98fde4

Browse files
committed
feat: add field_exists rule
1 parent 05fdab4 commit b98fde4

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
@@ -427,4 +427,22 @@ public function required_without(
427427

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

system/Validation/StrictRules/Rules.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,22 @@ public function required_without(
403403
): bool {
404404
return $this->nonStrictRules->required_without($str, $otherFields, $data, $error, $field);
405405
}
406+
407+
/**
408+
* The field exists in $data.
409+
*
410+
* @param array|bool|float|int|object|string|null $value The field value.
411+
* @param string|null $param The rule's parameter.
412+
* @param array $data The data to be validated.
413+
* @param string|null $field The field name.
414+
*/
415+
public function field_exists(
416+
$value = null,
417+
?string $param = null,
418+
array $data = [],
419+
?string $error = null,
420+
?string $field = null
421+
): bool {
422+
return (bool) (array_key_exists($field, $data));
423+
}
406424
}

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
@@ -851,4 +851,44 @@ public static function provideRequiredWithoutMultipleWithoutFields(): iterable
851851
],
852852
];
853853
}
854+
855+
/**
856+
* @dataProvider provideFieldExists
857+
*/
858+
public function testFieldExists(array $rules, array $data, bool $expected): void
859+
{
860+
$this->validation->setRules($rules);
861+
$this->assertSame($expected, $this->validation->run($data));
862+
}
863+
864+
public static function provideFieldExists(): iterable
865+
{
866+
yield from [
867+
[
868+
['foo' => 'field_exists'],
869+
['foo' => ''],
870+
true,
871+
],
872+
[
873+
['foo' => 'field_exists'],
874+
['foo' => null],
875+
true,
876+
],
877+
[
878+
['foo' => 'field_exists'],
879+
['foo' => false],
880+
true,
881+
],
882+
[
883+
['foo' => 'field_exists'],
884+
['foo' => []],
885+
true,
886+
],
887+
[
888+
['foo' => 'field_exists'],
889+
[],
890+
false,
891+
],
892+
];
893+
}
854894
}

0 commit comments

Comments
 (0)