@@ -215,11 +215,30 @@ protected function processRules(string $field, string $label = null, $value, $ru
215215
216216 if (in_array ('if_exist ' , $ rules , true ))
217217 {
218- // If the if_exist rule is defined
219- // and the current field does not exist in the input data
220- // we can return true, ignoring all other rules to this field.
221- if (! array_key_exists ($ field , array_flatten_with_dots ($ data )))
218+ $ flattenedData = array_flatten_with_dots ($ data );
219+ $ ifExistField = $ field ;
220+
221+ if (strpos ($ field , '.* ' ) !== false )
222+ {
223+ // We'll change the dot notation into a PCRE pattern
224+ // that can be used later
225+ $ ifExistField = str_replace ('\.\* ' , '\.(?:[^\.]+) ' , preg_quote ($ field , '/ ' ));
226+
227+ $ dataIsExisting = array_reduce (array_keys ($ flattenedData ), static function ($ carry , $ item ) use ($ ifExistField ) {
228+ $ pattern = sprintf ('/%s/u ' , $ ifExistField );
229+ return $ carry || preg_match ($ pattern , $ item ) === 1 ;
230+ }, false );
231+ }
232+ else
233+ {
234+ $ dataIsExisting = array_key_exists ($ ifExistField , $ flattenedData );
235+ }
236+
237+ unset($ ifExistField , $ flattenedData );
238+
239+ if (! $ dataIsExisting )
222240 {
241+ // we return early if `if_exist` is not satisfied. we have nothing to do here.
223242 return true ;
224243 }
225244
@@ -345,6 +364,7 @@ protected function processRules(string $field, string $label = null, $value, $ru
345364 */
346365 public function withRequest (RequestInterface $ request ): ValidationInterface
347366 {
367+ /** @var IncomingRequest $request */
348368 if (strpos ($ request ->getHeaderLine ('Content-Type ' ), 'application/json ' ) !== false )
349369 {
350370 $ this ->data = $ request ->getJSON (true );
@@ -363,7 +383,6 @@ public function withRequest(RequestInterface $request): ValidationInterface
363383 return $ this ;
364384 }
365385
366- //--------------------------------------------------------------------
367386 //--------------------------------------------------------------------
368387 // Rules
369388 //--------------------------------------------------------------------
@@ -812,24 +831,21 @@ protected function splitRules(string $rules): array
812831 {
813832 $ nonEscapeBracket = '((?<! \\\\)(?: \\\\\\\\)*[\[\]]) ' ;
814833 $ pipeNotInBracket = sprintf (
815- '/\|(?=(?:[^\[\]]*%s[^\[\]]*%s)*(?![^\[\]]*%s))/ ' ,
816- $ nonEscapeBracket ,
817- $ nonEscapeBracket ,
818- $ nonEscapeBracket
834+ '/\|(?=(?:[^\[\]]*%s[^\[\]]*%s)*(?![^\[\]]*%s))/ ' ,
835+ $ nonEscapeBracket ,
836+ $ nonEscapeBracket ,
837+ $ nonEscapeBracket
819838 );
820839
821- $ _rules = preg_split (
822- $ pipeNotInBracket ,
823- $ rules
824- );
840+ $ rules = preg_split ($ pipeNotInBracket , $ rules );
825841
826- return array_unique ($ _rules );
842+ return array_unique ($ rules );
827843 }
828844
829- //--------------------------------------------------------------------
830845 //--------------------------------------------------------------------
831846 // Misc
832847 //--------------------------------------------------------------------
848+
833849 /**
834850 * Resets the class to a blank slate. Should be called whenever
835851 * you need to process more than one array.
@@ -845,6 +861,4 @@ public function reset(): ValidationInterface
845861
846862 return $ this ;
847863 }
848-
849- //--------------------------------------------------------------------
850864}
0 commit comments