1010use Illuminate \Routing \UrlGenerator ;
1111use Illuminate \Support \Arr ;
1212use Illuminate \Support \Facades \Blade ;
13+ use Illuminate \Support \MessageBag ;
1314use Illuminate \Support \Str ;
1415use Illuminate \Support \ViewErrorBag ;
1516use Illuminate \Validation \ValidationException ;
@@ -54,8 +55,10 @@ public function handle(Request $request, Closure $next)
5455 $ this ->splade ->resetRehydrateComponentCounter ();
5556 $ this ->splade ->resetPersistentLayoutKey ();
5657
58+ /** @var Session */
5759 $ session = session ()->driver ();
58- $ session ->forget ('errors ' );
60+
61+ $ errorsFromRedirect = $ session ->pull ('errors ' , new ViewErrorBag );
5962
6063 /** @var Response $response */
6164 $ response = $ next ($ request );
@@ -74,7 +77,7 @@ public function handle(Request $request, Closure $next)
7477 }
7578
7679 // Gather the required meta data for the app.
77- $ spladeData = $ this ->spladeData ($ session );
80+ $ spladeData = $ this ->spladeData ($ session, $ errorsFromRedirect );
7881
7982 // The response should redirect away from the Splade app.
8083 if ($ redirect = $ this ->shouldRedirectsAway ($ response )) {
@@ -353,26 +356,47 @@ private function parseModalContent(string $content): ?string
353356 /**
354357 * Returns all error messages from the session.
355358 *
356- * @param \Illuminate\Contracts\Session\Session $session
359+ * @param \Illuminate\Support\ViewErrorBag $viewErrorBag
357360 * @return array
358361 */
359- private function allErrorMessages (Session $ session ): array
362+ private function allErrorMessages (ViewErrorBag $ viewErrorBag ): array
360363 {
361- /** @var ViewErrorBag */
362- $ viewErrorBag = $ session ->get ('errors ' , new ViewErrorBag );
363-
364364 return collect ($ viewErrorBag ->getBags ())
365365 ->flatMap ->getMessages ()
366366 ->toArray ();
367367 }
368368
369+ /**
370+ * Merges all bags from all view errors bags into one.
371+ *
372+ * @param \Illuminate\Support\ViewErrorBag[] ...$viewErrorsBags
373+ * @return \Illuminate\Support\ViewErrorBag
374+ */
375+ private function mergeViewErrorBags (...$ viewErrorsBags ): ViewErrorBag
376+ {
377+ $ mergedViewBag = new ViewErrorBag ;
378+
379+ collect ($ viewErrorsBags )->each (function (ViewErrorBag $ viewErrorBag ) use ($ mergedViewBag ) {
380+ collect ($ viewErrorBag ->getBags ())->each (function (MessageBag $ bag , string $ key ) use ($ mergedViewBag ) {
381+ $ mergedBag = $ mergedViewBag ->hasBag ($ key )
382+ ? $ mergedViewBag ->getBag ($ key )
383+ : tap (new MessageBag , fn ($ bag ) => $ mergedViewBag ->put ($ key , $ bag ));
384+
385+ $ mergedBag ->merge ($ bag );
386+ });
387+ });
388+
389+ return $ mergedViewBag ;
390+ }
391+
369392 /**
370393 * This methods returns all relevant data for a Splade page view.
371394 *
372395 * @param \Illuminate\Contracts\Session\Session $session
396+ * @param \Illuminate\Support\ViewErrorBag $errorsFromRedirect
373397 * @return object
374398 */
375- private function spladeData (Session $ session ): object
399+ private function spladeData (Session $ session, ViewErrorBag $ errorsFromRedirect ): object
376400 {
377401 $ flashData = config ('splade.share_session_flash_data ' )
378402 ? collect ($ session ->get ('_flash.old ' , []))
@@ -384,12 +408,17 @@ private function spladeData(Session $session): object
384408
385409 $ excludeHead = $ this ->splade ->isLazyRequest () || $ this ->splade ->isRehydrateRequest ();
386410
411+ $ mergedViewErrorBag = $ this ->mergeViewErrorBags (
412+ $ session ->get ('errors ' , new ViewErrorBag ),
413+ $ errorsFromRedirect
414+ );
415+
387416 return (object ) [
388417 'head ' => $ excludeHead ? [] : $ this ->splade ->head ()->toArray (),
389418 'modal ' => $ this ->splade ->isModalRequest () ? $ this ->splade ->getModalType () : null ,
390419 'modalTarget ' => $ this ->splade ->getModalTarget () ?: null ,
391420 'flash ' => (object ) $ flash ,
392- 'errors ' => (object ) $ this ->allErrorMessages ($ session ),
421+ 'errors ' => (object ) $ this ->allErrorMessages ($ mergedViewErrorBag ),
393422 'shared ' => (object ) Arr::map ($ this ->splade ->getShared (), fn ($ value ) => value ($ value )),
394423 'toasts ' => array_merge (
395424 $ session ->pull (static ::FLASH_TOASTS , []),
0 commit comments