@@ -307,11 +307,11 @@ class Validator implements ValidatorContract
307
307
protected $ defaultNumericRules = ['Numeric ' , 'Integer ' , 'Decimal ' ];
308
308
309
309
/**
310
- * The current placeholder for dots in rule keys .
310
+ * The current random hash for the validator .
311
311
*
312
312
* @var string
313
313
*/
314
- protected $ dotPlaceholder ;
314
+ protected static $ placeholderHash ;
315
315
316
316
/**
317
317
* The exception to throw upon failure.
@@ -344,7 +344,9 @@ public function __construct(
344
344
array $ messages = [],
345
345
array $ attributes = [],
346
346
) {
347
- $ this ->dotPlaceholder = Str::random ();
347
+ if (! isset (static ::$ placeholderHash )) {
348
+ static ::$ placeholderHash = Str::random ();
349
+ }
348
350
349
351
$ this ->initialRules = $ rules ;
350
352
$ this ->translator = $ translator ;
@@ -372,7 +374,7 @@ public function parseData(array $data)
372
374
373
375
$ key = str_replace (
374
376
['. ' , '* ' ],
375
- [$ this -> dotPlaceholder , '__asterisk__ ' ],
377
+ [' __dot__ ' . static :: $ placeholderHash , '__asterisk__ ' . static :: $ placeholderHash ],
376
378
$ key
377
379
);
378
380
@@ -410,7 +412,7 @@ protected function replacePlaceholders($data)
410
412
protected function replacePlaceholderInString (string $ value )
411
413
{
412
414
return str_replace (
413
- [$ this -> dotPlaceholder , '__asterisk__ ' ],
415
+ [' __dot__ ' . static :: $ placeholderHash , '__asterisk__ ' . static :: $ placeholderHash ],
414
416
['. ' , '* ' ],
415
417
$ value
416
418
);
@@ -425,7 +427,7 @@ protected function replacePlaceholderInString(string $value)
425
427
protected function replaceDotPlaceholderInParameters (array $ parameters )
426
428
{
427
429
return array_map (function ($ field ) {
428
- return str_replace ($ this -> dotPlaceholder , '. ' , $ field );
430
+ return str_replace (' __dot__ ' . static :: $ placeholderHash , '. ' , $ field );
429
431
}, $ parameters );
430
432
}
431
433
@@ -746,7 +748,7 @@ protected function getPrimaryAttribute($attribute)
746
748
protected function replaceDotInParameters (array $ parameters )
747
749
{
748
750
return array_map (function ($ field ) {
749
- return str_replace ('\. ' , $ this -> dotPlaceholder , $ field );
751
+ return str_replace ('\. ' , ' __dot__ ' . static :: $ placeholderHash , $ field );
750
752
}, $ parameters );
751
753
}
752
754
@@ -872,11 +874,24 @@ protected function hasNotFailedPreviousRuleIfPresenceRule($rule, $attribute)
872
874
*/
873
875
protected function validateUsingCustomRule ($ attribute , $ value , $ rule )
874
876
{
875
- $ attribute = $ this ->replacePlaceholderInString ($ attribute );
877
+ $ originalAttribute = $ this ->replacePlaceholderInString ($ attribute );
878
+
879
+ $ attribute = match (true ) {
880
+ $ rule instanceof Rules \Email => $ attribute ,
881
+ $ rule instanceof Rules \File => $ attribute ,
882
+ $ rule instanceof Rules \Password => $ attribute ,
883
+ default => $ originalAttribute ,
884
+ };
876
885
877
886
$ value = is_array ($ value ) ? $ this ->replacePlaceholders ($ value ) : $ value ;
878
887
879
888
if ($ rule instanceof ValidatorAwareRule) {
889
+ if ($ attribute !== $ originalAttribute ) {
890
+ $ this ->addCustomAttributes ([
891
+ $ attribute => $ this ->customAttributes [$ originalAttribute ] ?? $ originalAttribute ,
892
+ ]);
893
+ }
894
+
880
895
$ rule ->setValidator ($ this );
881
896
}
882
897
@@ -889,14 +904,14 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
889
904
get_class ($ rule ->invokable ()) :
890
905
get_class ($ rule );
891
906
892
- $ this ->failedRules [$ attribute ][$ ruleClass ] = [];
907
+ $ this ->failedRules [$ originalAttribute ][$ ruleClass ] = [];
893
908
894
- $ messages = $ this ->getFromLocalArray ($ attribute , $ ruleClass ) ?? $ rule ->message ();
909
+ $ messages = $ this ->getFromLocalArray ($ originalAttribute , $ ruleClass ) ?? $ rule ->message ();
895
910
896
911
$ messages = $ messages ? (array ) $ messages : [$ ruleClass ];
897
912
898
913
foreach ($ messages as $ key => $ message ) {
899
- $ key = is_string ($ key ) ? $ key : $ attribute ;
914
+ $ key = is_string ($ key ) ? $ key : $ originalAttribute ;
900
915
901
916
$ this ->messages ->add ($ key , $ this ->makeReplacements (
902
917
$ message , $ key , $ ruleClass , []
@@ -1189,7 +1204,7 @@ public function getRulesWithoutPlaceholders()
1189
1204
{
1190
1205
return (new Collection ($ this ->rules ))
1191
1206
->mapWithKeys (fn ($ value , $ key ) => [
1192
- str_replace ($ this -> dotPlaceholder , '\\. ' , $ key ) => $ value ,
1207
+ str_replace (' __dot__ ' . static :: $ placeholderHash , '\\. ' , $ key ) => $ value ,
1193
1208
])
1194
1209
->all ();
1195
1210
}
@@ -1203,7 +1218,7 @@ public function getRulesWithoutPlaceholders()
1203
1218
public function setRules (array $ rules )
1204
1219
{
1205
1220
$ rules = (new Collection ($ rules ))->mapWithKeys (function ($ value , $ key ) {
1206
- return [str_replace ('\. ' , $ this -> dotPlaceholder , $ key ) => $ value ];
1221
+ return [str_replace ('\. ' , ' __dot__ ' . static :: $ placeholderHash , $ key ) => $ value ];
1207
1222
})->toArray ();
1208
1223
1209
1224
$ this ->initialRules = $ rules ;
0 commit comments