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

HandlesValidationErrors   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasErrorAndShow() 0 6 2
A hasError() 0 8 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