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());
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
} |