Skip to content

Commit 997c7c9

Browse files
committed
Allow remove repeated optional rules
1 parent 8b3661a commit 997c7c9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Validation.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,16 +464,17 @@ protected function validateRule(string $rule, string $field, array $args, array
464464
*/
465465
protected function validateField(string $field, array $rules, array $data) : bool
466466
{
467+
$removeKeys = [];
467468
foreach ($rules as $key => $rule) {
468469
if ($rule['rule'] === 'optional') {
469-
$ruleKey = $key;
470+
$removeKeys[] = $key;
470471
if ( ! \array_key_exists($field, $data)) {
471472
return true;
472473
}
473474
}
474475
}
475-
if (isset($ruleKey)) {
476-
unset($rules[$ruleKey]);
476+
foreach ($removeKeys as $removeKey) {
477+
unset($rules[$removeKey]);
477478
}
478479
$status = true;
479480
foreach ($rules as $rule) {

tests/ValidationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ public function testOptionalAsLastRule() : void
527527
);
528528
}
529529

530+
public function testOptionalRepeated() : void
531+
{
532+
$this->validation->setRules([
533+
'email' => 'email|optional|optional',
534+
'name' => 'required',
535+
]);
536+
$status = $this->validation->validate([
537+
'email' => 'foo@bar.com',
538+
'name' => 'Jon',
539+
]);
540+
self::assertTrue($status);
541+
}
542+
530543
public function testEqualsField() : void
531544
{
532545
$this->validation->setRule('password', 'minLength:5');

0 commit comments

Comments
 (0)