1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoareCostin\BladeFormComponents; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ViewErrorBag; |
6
|
|
|
use SoareCostin\BladeFormComponents\Traits\GluesAttributes; |
7
|
|
|
|
8
|
|
|
abstract class FormElement |
9
|
|
|
{ |
10
|
|
|
use GluesAttributes; |
11
|
|
|
|
12
|
|
|
/** @var array */ |
13
|
|
|
protected $params; |
14
|
|
|
|
15
|
|
|
/** @var Form */ |
16
|
|
|
protected $form; |
17
|
|
|
|
18
|
|
|
/** @var object */ |
19
|
|
|
public $model = null; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
public $id; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public $name; |
26
|
|
|
|
27
|
|
|
/** @var mixed */ |
28
|
|
|
public $value; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public $label; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
public $class = []; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
public $labelClass = []; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
public $required = false; |
41
|
|
|
|
42
|
|
|
/** @var bool */ |
43
|
|
|
public $disabled = false; |
44
|
|
|
|
45
|
|
|
/** @var bool */ |
46
|
|
|
public $readonly = false; |
47
|
|
|
|
48
|
|
|
/** @var bool */ |
49
|
|
|
public $autocomplete; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
public $attributesList = []; |
53
|
|
|
|
54
|
|
|
/** @var string */ |
55
|
|
|
public $desc; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
public $help; |
59
|
|
|
|
60
|
|
|
public function __construct(array $params) |
61
|
|
|
{ |
62
|
|
|
$this->params = $params; |
63
|
|
|
|
64
|
|
|
$this->setForm(); |
65
|
|
|
$this->setModel(); |
66
|
|
|
$this->setCommonAttributes(); |
67
|
|
|
$this->setSpecificAttributes(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function setForm() |
71
|
|
|
{ |
72
|
|
|
$this->form = app(Form::class); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function setModel() |
76
|
|
|
{ |
77
|
|
|
if (isset($this->params['model']) && ! empty($this->params['model'])) { |
78
|
|
|
$this->model = $this->params['model']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Check to see if the model was added on the form opening tag |
82
|
|
|
if (is_null($this->model) && ! is_null($this->form->model)) { |
83
|
|
|
$this->model = $this->form->model; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function setCommonAttributes() |
88
|
|
|
{ |
89
|
|
|
$this->setName(); |
90
|
|
|
$this->setLabel(); |
91
|
|
|
$this->setValue(); |
92
|
|
|
$this->setClass(); |
93
|
|
|
$this->setRequired(); |
94
|
|
|
$this->setDisabled(); |
95
|
|
|
$this->setReadonly(); |
96
|
|
|
$this->setAutocomplete(); |
97
|
|
|
$this->setDesc(); |
98
|
|
|
$this->setHelp(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function setSpecificAttributes() |
102
|
|
|
{ |
103
|
|
|
// ... |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function setName() |
107
|
|
|
{ |
108
|
|
View Code Duplication |
if (isset($this->params['name']) && ! empty($this->params['name'])) { |
109
|
|
|
$this->name = $this->id = $this->params['name']; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function setLabel() |
114
|
|
|
{ |
115
|
|
|
// First, check if we receive an explicit label |
116
|
|
View Code Duplication |
if (isset($this->params['label']) && ! empty($this->params['label'])) { |
117
|
|
|
$this->label = $this->params['label']; |
118
|
|
|
|
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Check if we receive a label that is false, so we don't display it |
123
|
|
View Code Duplication |
if (isset($this->params['label']) && $this->params['label'] === false) { |
124
|
|
|
$this->label = false; |
|
|
|
|
125
|
|
|
|
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Fallback: construct the label from the name |
130
|
|
|
if (isset($this->name)) { |
131
|
|
|
$this->label = ucwords(str_replace('_', ' ', $this->name)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function setRequired() |
136
|
|
|
{ |
137
|
|
View Code Duplication |
if (isset($this->params['required']) && $this->params['required'] == true) { |
138
|
|
|
$this->required = true; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function setDisabled() |
143
|
|
|
{ |
144
|
|
View Code Duplication |
if (isset($this->params['disabled']) && $this->params['disabled'] == true) { |
145
|
|
|
$this->disabled = true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function setReadonly() |
150
|
|
|
{ |
151
|
|
View Code Duplication |
if (isset($this->params['readonly']) && $this->params['readonly'] == true) { |
152
|
|
|
$this->readonly = true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function setValue() |
157
|
|
|
{ |
158
|
|
|
if (is_null($this->name)) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (! is_null($this->value)) { |
163
|
|
|
$computedValue = $this->value; |
164
|
|
|
} elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) { |
165
|
|
|
$computedValue = $this->model->{str_replace('[]', '', $this->name)}; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->value = old($this->name, $computedValue ?? ''); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
protected function setAutocomplete() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
// Set default autocomplete option (true/false) from cofing file |
174
|
|
|
$this->autocomplete = config('blade-form-components.autocomplete'); |
175
|
|
|
|
176
|
|
|
if (isset($this->params['autocomplete'])) { |
177
|
|
|
$this->autocomplete = $this->params['autocomplete']; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function setDesc() |
182
|
|
|
{ |
183
|
|
|
if (isset($this->params['desc'])) { |
184
|
|
|
$this->desc = $this->params['desc']; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function setHelp() |
189
|
|
|
{ |
190
|
|
|
if (isset($this->params['help'])) { |
191
|
|
|
$this->help = $this->params['help']; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function setClass() |
196
|
|
|
{ |
197
|
|
|
$this->setDefaultClass(); |
198
|
|
|
|
199
|
|
|
// Attach the error class if an error is displayed against this field |
200
|
|
|
$errors = session()->get('errors', app(ViewErrorBag::class)); |
201
|
|
|
if ($errors->has($this->name)) { |
202
|
|
|
$this->class[] = config('blade-form-components.styles.field.error'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Attach other user-defined classes |
206
|
|
|
if (isset($this->params['class'])) { |
207
|
|
|
$this->class[] = $this->params['class']; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
protected function setDefaultClass() |
212
|
|
|
{ |
213
|
|
|
// Default class applied to all form elements (Eg 'form-control' for Bootstrap) |
214
|
|
|
$this->class[] = config('blade-form-components.styles.field.input'); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.