Skip to content

Commit e65c173

Browse files
authored
Merge pull request #15 from adelowo/rules
Internal rules can now be modified
2 parents 15f382c + e1362e0 commit e65c173

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Validator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Validator
1212

1313
protected $validators = [];
1414

15+
protected $allowRuleOverride = false;
16+
1517
public function __construct(array $messages = [])
1618
{
1719
$this->messages = $messages;
@@ -106,12 +108,17 @@ protected function registerBaseValidators()
106108

107109
public function addValidator($ruleName, Rule $rule)
108110
{
109-
if (array_key_exists($ruleName, $this->validators)) {
111+
if (!$this->allowRuleOverride && array_key_exists($ruleName, $this->validators)) {
110112
throw new RuleQuashException(
111113
"You cannot override a built in rule. You have to rename your rule"
112114
);
113115
}
114116

115117
$this->setValidator($ruleName, $rule);
116118
}
119+
120+
public function allowRuleOverride($status = false)
121+
{
122+
$this->allowRuleOverride = $status;
123+
}
117124
}

tests/ValidatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,21 @@ public function testInternalValidationRuleCannotBeOverridden()
347347
$validation->validate();
348348
}
349349

350+
public function testInternalValidationRuleCanBeOverridden()
351+
{
352+
$this->validator->allowRuleOverride(true);
353+
354+
$this->validator->addValidator('required', new Required()); //This is a custom rule defined in the fixtures directory
355+
356+
$data = ['s' => json_encode(['name' => 'space x', 'human' => false])];
357+
358+
$validation = $this->validator->make($data, ['s' => 'required'], []);
359+
360+
$validation->validate();
361+
362+
$this->assertTrue($validation->passes());
363+
}
364+
350365
public function testIgnoreNextRulesWhenImplicitRulesFails()
351366
{
352367
$validation = $this->validator->validate([

0 commit comments

Comments
 (0)