Skip to content

Commit

Permalink
Merge pull request #7815 from kenjis/fix-validation-rule-instances
Browse files Browse the repository at this point in the history
fix: instances of Validation rules are incremented each time `run()` is executed
  • Loading branch information
kenjis committed Aug 17, 2023
2 parents e5d25ea + 6ff41c9 commit 2d6eb27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public function __construct($config, RendererInterface $view)
$this->config = $config;

$this->view = $view;

$this->loadRuleSets();
}

/**
Expand All @@ -127,7 +129,6 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
// `DBGroup` is a reserved name. For is_unique and is_not_unique
$data['DBGroup'] = $dbGroup;

$this->loadRuleSets();
$this->loadRuleGroup($group);

// If no rules exist, we return false to ensure
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ public function testRunReturnsFalseWithNothingToDo(): void
$this->assertFalse($this->validation->run([]));
}

public function testRuleClassesInstantiatedOnce(): void
{
$this->validation->setRules([]);
$this->validation->run([]);
$count1 = count(
$this->getPrivateProperty($this->validation, 'ruleSetInstances')
);

$this->validation->run([]);
$count2 = count(
$this->getPrivateProperty($this->validation, 'ruleSetInstances')
);

$this->assertSame($count1, $count2);
}

public function testRunDoesTheBasics(): void
{
$data = ['foo' => 'notanumber'];
Expand Down

0 comments on commit 2d6eb27

Please sign in to comment.