Passed
Pull Request — master (#25)
by
unknown
02:27
created

FormValidator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 156
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasError() 0 9 2
A getGeneralRules() 0 4 1
A validate() 0 35 6
A validateAndRedirectBack() 0 3 1
A getRules() 0 5 4
A add() 0 22 5
A getUpdateRules() 0 4 1
A addDefaultActions() 0 17 1
1
<?php namespace Distilleries\FormBuilder;
2
3
use \Validator;
4
use \Redirect;
5
6
class FormValidator extends FormView {
7
8
    public static $rules = [];
9
    public static $rules_update = null;
10
11
    // ------------------------------------------------------------------------------------------------
12
13
    protected $hasError = false;
14
    protected $validation = null;
15
    protected $formOptions = [
16
        'method' => 'POST',
17
        'url'    => null,
18
    ];
19
20
    // ------------------------------------------------------------------------------------------------
21
    // ------------------------------------------------------------------------------------------------
22
    // ------------------------------------------------------------------------------------------------
23
24
    /**
25
     * @param string $name
26
     * @param string $type
27
     * @param array $options
28
     * @param bool $modify
29
     * @param bool $noOveride
30
     * @return $this
31
     */
32 140
    public function add($name, $type = 'text', array $options = [], $modify = false, $noOveride = false)
33
    {
34
35 140
        $defaultClass = $this->formHelper->getConfig('defaults.field_class').' ';
36 140
        if (empty($options['attr']['class']))
37
        {
38 140
            $options['attr']['class'] = '';
39
        }
40
41 140
        if (empty($noOveride))
42
        {
43 140
            $options['attr']['class'] = $defaultClass.' '.$options['attr']['class'].' ';
44
        }
45
46 140
        if (!empty($options) && isset($options['validation']))
47
        {
48
49 36
            $options['attr']['class'] .= ' validate['.$options['validation'].']'.' ';
50 36
            unset($options['validation']);
51
        }
52
53 140
        return parent::add($name, $type, $options, $modify);
54
    }
55
56
    // ------------------------------------------------------------------------------------------------
57
58 12
    public function validateAndRedirectBack()
59
    {
60 12
        return Redirect::back()->withErrors($this->validate())->withInput($this->formHelper->getRequest()->all());
61
62
    }
63
64
    // ------------------------------------------------------------------------------------------------
65
66 40
    public function validate()
67
    {
68 40
        if ($this->validation == null)
69
        {
70
71 40
            $fields = $this->getFields();
72
73 40
            foreach ($fields as $field)
74
            {
75 40
                if ($field->getType() == 'form')
76
                {
77
78 8
                    $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

78
                    $validation = Validator::make($this->formHelper->getRequest()->get($field->getName(),[]), $field->/** @scrutinizer ignore-call */ getClass()->getRules());
Loading history...
79
80 8
                    if ($validation->fails())
81
                    {
82 4
                        $this->hasError   = true;
83 4
                        $this->validation = $validation;
84
85 40
                        return $this->validation;
86
                    }
87
                }
88
            }
89
90 36
            $validation = Validator::make($this->formHelper->getRequest()->all(), $this->getRules());
91
92 36
            if ($validation->fails())
93
            {
94 8
                $this->hasError = true;
95
            }
96
97 36
            $this->validation = $validation;
98
        }
99
100 40
        return $this->validation;
101
    }
102
103
    // ------------------------------------------------------------------------------------------------
104
105 40
    public function hasError()
106
    {
107
108 40
        if ($this->validation == null)
109
        {
110 40
            $this->validate();
111
        }
112
113 40
        return $this->hasError;
114
    }
115
116
117
    // ------------------------------------------------------------------------------------------------
118
119 64
    public function addDefaultActions()
120
    {
121 64
        $this->add('submit', 'submit',
122
            [
123 64
                'label' => trans('form-builder::form.save'),
124
                'attr'  => [
125
                    'class' => 'btn green'
126
                ],
127 64
            ], false, true)
128 64
            ->add('back', 'button',
129
                [
130 64
                    'label' => trans('form-builder::form.back'),
131
                    'attr'  => [
132
                        'class'   => 'btn default',
133
                        'onclick' => 'window.history.back()'
134
                    ],
135 64
                ], false, true);
136
137
    }
138
139
    // ------------------------------------------------------------------------------------------------
140
141 40
    protected function getRules()
142
    {
143 40
        $key = !empty($this->model) ? $this->formHelper->getRequest()->get($this->model->getKeyName()) : null;
144
145 40
        return ($this->getUpdateRules() == null || empty($key)) ? $this->getGeneralRules() : $this->getUpdateRules();
146
    }
147
148
    // ------------------------------------------------------------------------------------------------
149
150 36
    protected function getGeneralRules()
151
    {
152
153 36
        return static::$rules;
154
    }
155
156
    // ------------------------------------------------------------------------------------------------
157
158 40
    protected function getUpdateRules()
159
    {
160
161 40
        return static::$rules_update;
162
    }
163
}