22
33namespace ProtoneMedia \LaravelFormComponents \Components ;
44
5+ use Illuminate \Contracts \Support \MessageBag ;
56use Illuminate \Support \Facades \View ;
7+ use Illuminate \Support \Str ;
68use Illuminate \Support \ViewErrorBag ;
79
810trait HandlesValidationErrors
@@ -24,6 +26,19 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
2426 : false ;
2527 }
2628
29+ /**
30+ * Getter for the ErrorBag.
31+ *
32+ * @param string $bag
33+ * @return \Illuminate\Contracts\Support\MessageBag
34+ */
35+ protected function getErrorBag (string $ bag = 'default ' ): MessageBag
36+ {
37+ $ bags = View::shared ('errors ' , fn () => request ()->session ()->get ('errors ' , new ViewErrorBag ));
38+
39+ return $ bags ->getBag ($ bag );
40+ }
41+
2742 /**
2843 * Returns a boolean wether the given attribute has an error.
2944 *
@@ -33,10 +48,10 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
3348 */
3449 public function hasError (string $ name , string $ bag = 'default ' ): bool
3550 {
36- $ errors = View:: shared ( ' errors ' , fn () => request ()-> session ()-> get ( ' errors ' , new ViewErrorBag ));
51+ $ name = str_replace ([ ' [ ' , ' ] ' ], [ ' . ' , '' ], Str:: before ( $ name , ' [] ' ));
3752
38- $ name = str_replace ([ ' [ ' , ' ] ' ], [ ' . ' , '' ], $ name );
53+ $ errorBag = $ this -> getErrorBag ( $ bag );
3954
40- return $ errors -> getBag ( $ bag ) ->has ($ name );
55+ return $ errorBag -> has ( $ name ) || $ errorBag ->has ($ name . ' .* ' );
4156 }
4257}
0 commit comments