Completed
Push — master ( 936da0...f7c12a )
by Pascal
14:46 queued 13:50
created

HandlesValidationErrors::hasErrorAndShow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
use Illuminate\Support\Facades\View;
6
use Illuminate\Support\ViewErrorBag;
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));
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DOUBLE_ARROW, expecting ',' or ')'
Loading history...
37
38
        $name = str_replace(['[', ']'], ['.', ''], $name);
39
40
        return $errors->getBag($bag)->has($name);
41
    }
42
}
43