Completed
Push — master ( 889c40...2252b0 )
by Pascal
01:09 queued 11s
created

HandlesValidationErrors::hasErrorAndShow()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.8666
cc 3
nc 3
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
    public function hasErrorAndShow(string $name, string $bag = 'default'): bool
12
    {
13
        if (!$this->showErrors) {
14
            return false;
15
        }
16
17
        $errors = request()->session()->get('errors') ?: new ViewErrorBag;
18
19
        $name = str_replace(['[', ']'], ['.', ''], $name);
20
21
        return $errors->getBag($bag)->has($name);
22
    }
23
}
24