FormValidator::addDefaultActions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php namespace Distilleries\FormBuilder;
2
3
use \Validator;
4
use \Redirect;
5
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
6
7
class FormValidator extends FormView {
8
9
    public static $rules = [];
10
    public static $rules_update = null;
11
12
    // ------------------------------------------------------------------------------------------------
13
14
    protected $hasError = false;
15
    protected $validation = null;
16
    protected $formOptions = [
17
        'method' => 'POST',
18
        'url'    => null,
19
    ];
20
21
    // ------------------------------------------------------------------------------------------------
22
    // ------------------------------------------------------------------------------------------------
23
    // ------------------------------------------------------------------------------------------------
24
25
    /**
26
     * @param string $name
27
     * @param string $type
28
     * @param array $options
29
     * @param bool $modify
30
     * @param bool $noOveride
31
     * @return $this
32
     */
33 72
    public function add($name, $type = 'text', array $options = [], $modify = false, $noOveride = false)
34
    {
35
36 72
        $defaultClass = $this->formHelper->getConfig('defaults.field_class').' ';
37 72
        if (empty($options['attr']['class']))
38
        {
39 72
            $options['attr']['class'] = '';
40
        }
41
42 72
        if (empty($noOveride))
43
        {
44 72
            $options['attr']['class'] = $defaultClass.' '.$options['attr']['class'].' ';
45
        }
46
47 72
        if (!empty($options) && isset($options['validation']))
48
        {
49
50 20
            $options['attr']['class'] .= ' validate['.$options['validation'].']'.' ';
51 20
            unset($options['validation']);
52
        }
53
54 72
        return parent::add($name, $type, $options, $modify);
55
    }
56
57
    // ------------------------------------------------------------------------------------------------
58
59 8
    public function validateAndRedirectBack()
60
    {
61 8
        return Redirect::back()->withErrors($this->validate())->withInput($this->formHelper->getRequest()->all());
62
63
    }
64
65
    // ------------------------------------------------------------------------------------------------
66
67 22
    public function validate()
68
    {
69 22
        if ($this->validation == null)
70
        {
71
72 22
            $fields = $this->getFields();
73
74 22
            foreach ($fields as $field)
75
            {
76 22
                if ($field->getType() == 'form')
77
                {
78
79 4
                    $validation = Validator::make($this->formHelper->getRequest()->get($field->getName(),[]), $field->getClass()->getRules());
0 ignored issues
show
Bug introduced by
The method getClass() does not exist on Kris\LaravelFormBuilder\Fields\FormField. It seems like you code against a sub-type of Kris\LaravelFormBuilder\Fields\FormField such as Kris\LaravelFormBuilder\Fields\ChildFormType or Distilleries\FormBuilder\Fields\ChildFormType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
                    $validation = Validator::make($this->formHelper->getRequest()->get($field->getName(),[]), $field->/** @scrutinizer ignore-call */ getClass()->getRules());
Loading history...
80
81 4
                    if ($validation->fails())
82
                    {
83 2
                        $this->hasError   = true;
84 2
                        $this->validation = $validation;
85
86 2
                        return $this->validation;
87
                    }
88
                }
89
            }
90
91 20
            $validation = Validator::make($this->formHelper->getRequest()->all(), $this->getRules());
92
93
            $validation->after(function ($validator) {
94 20
                $this->afterValidate($validator, $this->formHelper->getRequest()->all());
95 20
            });
96
97 20
            if ($validation->fails())
98
            {
99 6
                $this->hasError = true;
100
            }
101
102 20
            $this->validation = $validation;
103
        }
104
105 22
        return $this->validation;
106
    }
107
108
    // ------------------------------------------------------------------------------------------------
109
110 10
    protected function afterValidate(ValidatorContract $validator, array $inputs)
0 ignored issues
show
Unused Code introduced by
The parameter $inputs is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    protected function afterValidate(ValidatorContract $validator, /** @scrutinizer ignore-unused */ array $inputs)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112 10
        return;
113
    }
114
    
115
    // ------------------------------------------------------------------------------------------------
116
117 22
    public function hasError()
118
    {
119
120 22
        if ($this->validation == null)
121
        {
122 22
            $this->validate();
123
        }
124
125 22
        return $this->hasError;
126
    }
127
128
129
    // ------------------------------------------------------------------------------------------------
130
131 34
    public function addDefaultActions()
132
    {
133 34
        $this->add('submit', 'submit',
134
            [
135 34
                'label' => trans('form-builder::form.save'),
136
                'attr'  => [
137
                    'class' => 'btn green'
138
                ],
139 34
            ], false, true)
140 34
            ->add('back', 'button',
141
                [
142 34
                    'label' => trans('form-builder::form.back'),
143
                    'attr'  => [
144
                        'class'   => 'btn default',
145
                        'onclick' => 'window.history.back()'
146
                    ],
147 34
                ], false, true);
148
149
    }
150
151
    // ------------------------------------------------------------------------------------------------
152
153 22
    protected function getRules()
154
    {
155 22
        $key = !empty($this->model) ? $this->formHelper->getRequest()->get($this->model->getKeyName()) : null;
156
157 22
        return ($this->getUpdateRules() == null || empty($key)) ? $this->getGeneralRules() : $this->getUpdateRules();
158
    }
159
160
    // ------------------------------------------------------------------------------------------------
161
162 18
    protected function getGeneralRules()
163
    {
164
165 18
        return static::$rules;
166
    }
167
168
    // ------------------------------------------------------------------------------------------------
169
170 22
    protected function getUpdateRules()
171
    {
172
173 22
        return static::$rules_update;
174
    }
175
}