File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ class Validator
12
12
13
13
protected $ validators = [];
14
14
15
+ protected $ allowRuleOverride = false ;
16
+
15
17
public function __construct (array $ messages = [])
16
18
{
17
19
$ this ->messages = $ messages ;
@@ -106,12 +108,17 @@ protected function registerBaseValidators()
106
108
107
109
public function addValidator ($ ruleName , Rule $ rule )
108
110
{
109
- if (array_key_exists ($ ruleName , $ this ->validators )) {
111
+ if (! $ this -> allowRuleOverride && array_key_exists ($ ruleName , $ this ->validators )) {
110
112
throw new RuleQuashException (
111
113
"You cannot override a built in rule. You have to rename your rule "
112
114
);
113
115
}
114
116
115
117
$ this ->setValidator ($ ruleName , $ rule );
116
118
}
119
+
120
+ public function allowRuleOverride ($ status = false )
121
+ {
122
+ $ this ->allowRuleOverride = $ status ;
123
+ }
117
124
}
Original file line number Diff line number Diff line change @@ -347,6 +347,21 @@ public function testInternalValidationRuleCannotBeOverridden()
347
347
$ validation ->validate ();
348
348
}
349
349
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
+
350
365
public function testIgnoreNextRulesWhenImplicitRulesFails ()
351
366
{
352
367
$ validation = $ this ->validator ->validate ([
You can’t perform that action at this time.
0 commit comments