1 | <?php |
||
7 | |||
8 | trait HandlesValidationErrors |
||
9 | { |
||
10 | public $showErrors = true; |
||
11 | |||
12 | /** |
||
13 | * Returns a boolean wether the given attribute has an error |
||
14 | * and the should be shown. |
||
15 | * |
||
16 | * @param string $name |
||
17 | * @param string $bag |
||
18 | * @return boolean |
||
19 | */ |
||
20 | public function hasErrorAndShow(string $name, string $bag = 'default'): bool |
||
21 | { |
||
22 | return $this->showErrors |
||
23 | ? $this->hasError($name, $bag) |
||
24 | : false; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Returns a boolean wether the given attribute has an error. |
||
29 | * |
||
30 | * @param string $name |
||
31 | * @param string $bag |
||
32 | * @return boolean |
||
33 | */ |
||
34 | public function hasError(string $name, string $bag = 'default'): bool |
||
35 | { |
||
36 | $errors = View::shared('errors', fn () => request()->session()->get('errors', new ViewErrorBag)); |
||
|
|||
37 | |||
38 | $name = str_replace(['[', ']'], ['.', ''], $name); |
||
39 | |||
40 | return $errors->getBag($bag)->has($name); |
||
41 | } |
||
42 | } |
||
43 |