Completed
Push — master ( 2252b0...f037af )
by Pascal
02:07 queued 01:06
created

HandlesValidationErrors::hasError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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