2
2
3
3
namespace ProtoneMedia \LaravelFormComponents \Components ;
4
4
5
+ use Illuminate \Contracts \Support \MessageBag ;
5
6
use Illuminate \Support \Facades \View ;
7
+ use Illuminate \Support \Str ;
6
8
use Illuminate \Support \ViewErrorBag ;
7
9
8
10
trait HandlesValidationErrors
@@ -24,6 +26,19 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
24
26
: false ;
25
27
}
26
28
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
+
27
42
/**
28
43
* Returns a boolean wether the given attribute has an error.
29
44
*
@@ -33,10 +48,10 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
33
48
*/
34
49
public function hasError (string $ name , string $ bag = 'default ' ): bool
35
50
{
36
- $ errors = View:: shared ( ' errors ' , fn () => request ()-> session ()-> get ( ' errors ' , new ViewErrorBag ));
51
+ $ name = str_replace ([ ' [ ' , ' ] ' ], [ ' . ' , '' ], Str:: before ( $ name , ' [] ' ));
37
52
38
- $ name = str_replace ([ ' [ ' , ' ] ' ], [ ' . ' , '' ], $ name );
53
+ $ errorBag = $ this -> getErrorBag ( $ bag );
39
54
40
- return $ errors -> getBag ( $ bag ) ->has ($ name );
55
+ return $ errorBag -> has ( $ name ) || $ errorBag ->has ($ name . ' .* ' );
41
56
}
42
57
}
0 commit comments