@@ -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 );
@@ -365,7 +385,6 @@ public function withRequest(RequestInterface $request): ValidationInterface
365385 return $ this ;
366386 }
367387
368- //--------------------------------------------------------------------
369388 //--------------------------------------------------------------------
370389 // Rules
371390 //--------------------------------------------------------------------
@@ -814,24 +833,21 @@ protected function splitRules(string $rules): array
814833 {
815834 $ nonEscapeBracket = '((?<! \\\\)(?: \\\\\\\\)*[\[\]]) ' ;
816835 $ pipeNotInBracket = sprintf (
817- '/\|(?=(?:[^\[\]]*%s[^\[\]]*%s)*(?![^\[\]]*%s))/ ' ,
818- $ nonEscapeBracket ,
819- $ nonEscapeBracket ,
820- $ nonEscapeBracket
836+ '/\|(?=(?:[^\[\]]*%s[^\[\]]*%s)*(?![^\[\]]*%s))/ ' ,
837+ $ nonEscapeBracket ,
838+ $ nonEscapeBracket ,
839+ $ nonEscapeBracket
821840 );
822841
823- $ _rules = preg_split (
824- $ pipeNotInBracket ,
825- $ rules
826- );
842+ $ _rules = preg_split ($ pipeNotInBracket , $ rules );
827843
828844 return array_unique ($ _rules );
829845 }
830846
831- //--------------------------------------------------------------------
832847 //--------------------------------------------------------------------
833848 // Misc
834849 //--------------------------------------------------------------------
850+
835851 /**
836852 * Resets the class to a blank slate. Should be called whenever
837853 * you need to process more than one array.
@@ -847,6 +863,4 @@ public function reset(): ValidationInterface
847863
848864 return $ this ;
849865 }
850-
851- //--------------------------------------------------------------------
852866}
0 commit comments