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