Form   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 7
c 1
b 0
f 0
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasError() 0 5 1
A __construct() 0 5 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
use Illuminate\Support\Facades\View;
6
use Illuminate\Support\ViewErrorBag;
7
8
class Form extends Component
9
{
10
    /**
11
     * Request method.
12
     */
13
    public string $method;
14
15
    /**
16
     * Form method spoofing to support PUT, PATCH and DELETE actions.
17
     * https://laravel.com/docs/master/routing#form-method-spoofing
18
     */
19
    public bool $spoofMethod = false;
20
21
    /**
22
     * Create a new component instance.
23
     *
24
     * @return void
25
     */
26
    public function __construct(string $method = 'POST')
27
    {
28
        $this->method = strtoupper($method);
29
30
        $this->spoofMethod = in_array($this->method, ['PUT', 'PATCH', 'DELETE']);
31
    }
32
33
    /**
34
     * Returns a boolean wether the error bag is not empty.
35
     *
36
     * @param string $bag
37
     * @return boolean
38
     */
39
    public function hasError($bag = 'default'): bool
40
    {
41
        $errors = View::shared('errors', fn () => request()->session()->get('errors', new ViewErrorBag));
42
43
        return $errors->getBag($bag)->isNotEmpty();
44
    }
45
}
46