@@ -155,6 +155,10 @@ public function run(array $data = null, string $group = null, string $db_group =
155155 return false ;
156156 }
157157
158+ // Replace any placeholders (i.e. {id}) in the rules with
159+ // the value found in $data, if exists.
160+ $ this ->rules = $ this ->fillPlaceholders ($ this ->rules , $ data );
161+
158162 // Need this for searching arrays in validation.
159163 helper ('array ' );
160164
@@ -603,6 +607,64 @@ public function loadRuleGroup(string $group = null)
603607 return $ this ->rules ;
604608 }
605609
610+ //--------------------------------------------------------------------
611+
612+ /**
613+ * Replace any placeholders within the rules with the values that
614+ * match the 'key' of any properties being set. For example, if
615+ * we had the following $data array:
616+ *
617+ * [ 'id' => 13 ]
618+ *
619+ * and the following rule:
620+ *
621+ * 'required|is_unique[users,email,id,{id}]'
622+ *
623+ * The value of {id} would be replaced with the actual id in the form data:
624+ *
625+ * 'required|is_unique[users,email,id,13]'
626+ *
627+ * @param array $rules
628+ * @param array $data
629+ *
630+ * @return array
631+ */
632+ protected function fillPlaceholders (array $ rules , array $ data ): array
633+ {
634+ $ replacements = [];
635+
636+ foreach ($ data as $ key => $ value )
637+ {
638+ $ replacements ["{ {$ key }} " ] = $ value ;
639+ }
640+
641+ if (! empty ($ replacements ))
642+ {
643+ foreach ($ rules as &$ rule )
644+ {
645+ if (is_array ($ rule ))
646+ {
647+ foreach ($ rule as &$ row )
648+ {
649+ // Should only be an `errors` array
650+ // which doesn't take placeholders.
651+ if (is_array ($ row ))
652+ {
653+ continue ;
654+ }
655+
656+ $ row = strtr ($ row , $ replacements );
657+ }
658+ continue ;
659+ }
660+
661+ $ rule = strtr ($ rule , $ replacements );
662+ }
663+ }
664+
665+ return $ rules ;
666+ }
667+
606668 //--------------------------------------------------------------------
607669 //--------------------------------------------------------------------
608670 // Errors
0 commit comments