1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\MessageBag; |
6
|
|
|
use Illuminate\Contracts\View\Factory as View; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Illuminate\Translation\Translator; |
11
|
|
|
use Kris\LaravelFormBuilder\Events\AfterCollectingFieldRules; |
12
|
|
|
use Kris\LaravelFormBuilder\Fields\CheckableType; |
13
|
|
|
use Kris\LaravelFormBuilder\Fields\FormField; |
14
|
|
|
use Kris\LaravelFormBuilder\Form; |
15
|
|
|
use Kris\LaravelFormBuilder\RulesParser; |
16
|
|
|
|
17
|
|
|
class FormHelper |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var View |
22
|
|
|
*/ |
23
|
|
|
protected $view; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TranslatorInterface |
27
|
|
|
*/ |
28
|
|
|
protected $translator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FormBuilder |
37
|
|
|
*/ |
38
|
|
|
protected $formBuilder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected static $reservedFieldNames = [ |
44
|
|
|
'save' |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* All available field types |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected static $availableFieldTypes = [ |
53
|
|
|
'text' => 'InputType', |
54
|
|
|
'email' => 'InputType', |
55
|
|
|
'url' => 'InputType', |
56
|
|
|
'tel' => 'InputType', |
57
|
|
|
'search' => 'InputType', |
58
|
|
|
'password' => 'InputType', |
59
|
|
|
'hidden' => 'InputType', |
60
|
|
|
'number' => 'InputType', |
61
|
|
|
'date' => 'InputType', |
62
|
|
|
'file' => 'InputType', |
63
|
|
|
'image' => 'InputType', |
64
|
|
|
'color' => 'InputType', |
65
|
|
|
'datetime-local' => 'InputType', |
66
|
|
|
'month' => 'InputType', |
67
|
|
|
'range' => 'InputType', |
68
|
|
|
'time' => 'InputType', |
69
|
|
|
'week' => 'InputType', |
70
|
|
|
'select' => 'SelectType', |
71
|
|
|
'textarea' => 'TextareaType', |
72
|
|
|
'button' => 'ButtonType', |
73
|
|
|
'buttongroup' => 'ButtonGroupType', |
74
|
|
|
'submit' => 'ButtonType', |
75
|
|
|
'reset' => 'ButtonType', |
76
|
|
|
'radio' => 'CheckableType', |
77
|
|
|
'checkbox' => 'CheckableType', |
78
|
|
|
'choice' => 'ChoiceType', |
79
|
|
|
'form' => 'ChildFormType', |
80
|
|
|
'entity' => 'EntityType', |
81
|
|
|
'collection' => 'CollectionType', |
82
|
|
|
'repeated' => 'RepeatedType', |
83
|
|
|
'static' => 'StaticType' |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Custom types |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
private $customTypes = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param View $view |
95
|
|
|
* @param Translator $translator |
96
|
|
|
* @param array $config |
97
|
134 |
|
*/ |
98
|
|
|
public function __construct(View $view, Translator $translator, array $config = []) |
99
|
134 |
|
{ |
100
|
134 |
|
$this->view = $view; |
101
|
134 |
|
$this->translator = $translator; |
|
|
|
|
102
|
134 |
|
$this->config = $config; |
103
|
134 |
|
$this->loadCustomTypes(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $key |
108
|
|
|
* @param string $default |
109
|
|
|
* @param array $customConfig |
110
|
|
|
* @return mixed |
111
|
134 |
|
*/ |
112
|
|
|
public function getConfig($key = null, $default = null, $customConfig = []) |
113
|
134 |
|
{ |
114
|
|
|
$config = array_replace_recursive($this->config, $customConfig); |
115
|
134 |
|
|
116
|
134 |
|
if ($key) { |
|
|
|
|
117
|
|
|
return Arr::get($config, $key, $default); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $config; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return View |
125
|
38 |
|
*/ |
126
|
|
|
public function getView() |
127
|
38 |
|
{ |
128
|
|
|
return $this->view; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Merge options array. |
133
|
|
|
* |
134
|
|
|
* @param array $targetOptions |
135
|
|
|
* @param array $sourceOptions |
136
|
|
|
* @return array |
137
|
134 |
|
*/ |
138
|
|
|
public function mergeOptions(array $targetOptions, array $sourceOptions) |
139
|
134 |
|
{ |
140
|
|
|
return array_replace_recursive($targetOptions, $sourceOptions); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get proper class for field type. |
146
|
|
|
* |
147
|
|
|
* @param $type |
148
|
|
|
* @return string |
149
|
91 |
|
*/ |
150
|
|
|
public function getFieldType($type) |
151
|
91 |
|
{ |
152
|
|
|
$types = array_keys(static::$availableFieldTypes); |
153
|
91 |
|
|
154
|
1 |
|
if (!$type || trim($type) == '') { |
155
|
|
|
throw new \InvalidArgumentException('Field type must be provided.'); |
156
|
|
|
} |
157
|
90 |
|
|
158
|
3 |
|
if ($this->hasCustomField($type)) { |
159
|
|
|
return $this->customTypes[$type]; |
160
|
|
|
} |
161
|
87 |
|
|
162
|
2 |
|
if (!in_array($type, $types)) { |
163
|
2 |
|
throw new \InvalidArgumentException( |
164
|
2 |
|
sprintf( |
165
|
2 |
|
'Unsupported field type [%s]. Available types are: %s', |
166
|
2 |
|
$type, |
167
|
|
|
join(', ', array_merge($types, array_keys($this->customTypes))) |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
85 |
|
|
172
|
|
|
$namespace = __NAMESPACE__.'\\Fields\\'; |
173
|
85 |
|
|
174
|
|
|
return $namespace . static::$availableFieldTypes[$type]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Convert array of attributes to html attributes. |
179
|
|
|
* |
180
|
|
|
* @param $options |
181
|
|
|
* @return string |
182
|
107 |
|
*/ |
183
|
|
|
public function prepareAttributes($options) |
184
|
107 |
|
{ |
185
|
13 |
|
if (!$options) { |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
107 |
|
|
189
|
|
|
$attributes = []; |
190
|
107 |
|
|
191
|
107 |
|
foreach ($options as $name => $option) { |
192
|
107 |
|
if ($option !== null) { |
193
|
107 |
|
$name = is_numeric($name) ? $option : $name; |
194
|
|
|
$attributes[] = $name.'="'.$option.'" '; |
195
|
|
|
} |
196
|
|
|
} |
197
|
107 |
|
|
198
|
|
|
return join('', $attributes); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Add custom field. |
203
|
|
|
* |
204
|
|
|
* @param $name |
205
|
|
|
* @param $class |
206
|
4 |
|
*/ |
207
|
|
|
public function addCustomField($name, $class) |
208
|
4 |
|
{ |
209
|
4 |
|
if (!$this->hasCustomField($name)) { |
210
|
|
|
return $this->customTypes[$name] = $class; |
211
|
|
|
} |
212
|
1 |
|
|
213
|
|
|
throw new \InvalidArgumentException('Custom field ['.$name.'] already exists on this form object.'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Load custom field types from config file. |
218
|
134 |
|
*/ |
219
|
|
|
private function loadCustomTypes() |
220
|
134 |
|
{ |
221
|
|
|
$customFields = (array) $this->getConfig('custom_fields'); |
222
|
134 |
|
|
223
|
1 |
|
if (!empty($customFields)) { |
224
|
1 |
|
foreach ($customFields as $fieldName => $fieldClass) { |
225
|
|
|
$this->addCustomField($fieldName, $fieldClass); |
226
|
|
|
} |
227
|
134 |
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Check if custom field with provided name exists |
232
|
|
|
* @param string $name |
233
|
|
|
* @return boolean |
234
|
91 |
|
*/ |
235
|
|
|
public function hasCustomField($name) |
236
|
91 |
|
{ |
237
|
|
|
return array_key_exists($name, $this->customTypes); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param object $model |
242
|
|
|
* @return object|null |
243
|
5 |
|
*/ |
244
|
|
|
public function convertModelToArray($model) |
245
|
5 |
|
{ |
246
|
1 |
|
if (!$model) { |
247
|
|
|
return null; |
248
|
|
|
} |
249
|
5 |
|
|
250
|
3 |
|
if ($model instanceof Model) { |
251
|
|
|
return $model->toArray(); |
252
|
|
|
} |
253
|
3 |
|
|
254
|
2 |
|
if ($model instanceof Collection) { |
255
|
|
|
return $model->all(); |
256
|
|
|
} |
257
|
2 |
|
|
258
|
|
|
return $model; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Format the label to the proper format. |
263
|
|
|
* |
264
|
|
|
* @param $name |
265
|
|
|
* @return string |
266
|
105 |
|
*/ |
267
|
|
|
public function formatLabel($name) |
268
|
105 |
|
{ |
269
|
1 |
|
if (!$name) { |
270
|
|
|
return null; |
271
|
|
|
} |
272
|
105 |
|
|
273
|
2 |
|
if ($this->translator->has($name)) { |
274
|
|
|
$translatedName = $this->translator->get($name); |
275
|
2 |
|
|
276
|
2 |
|
if (is_string($translatedName)) { |
277
|
|
|
return $translatedName; |
278
|
|
|
} |
279
|
|
|
} |
280
|
103 |
|
|
281
|
|
|
return ucfirst(str_replace('_', ' ', $name)); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param FormField $field |
286
|
|
|
* @return RulesParser |
287
|
106 |
|
*/ |
288
|
|
|
public function createRulesParser(FormField $field) |
289
|
106 |
|
{ |
290
|
|
|
return new RulesParser($field); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param FormField $field |
295
|
|
|
* @return array |
296
|
10 |
|
*/ |
297
|
|
|
public function getFieldValidationRules(FormField $field) |
298
|
10 |
|
{ |
299
|
|
|
$fieldRules = $field->getValidationRules(); |
300
|
10 |
|
|
301
|
|
|
if (is_array($fieldRules)) { |
302
|
|
|
$fieldRules = Rules::fromArray($fieldRules)->setFieldName($field->getNameKey()); |
303
|
|
|
} |
304
|
10 |
|
|
305
|
10 |
|
$formBuilder = $field->getParent()->getFormBuilder(); |
306
|
|
|
$formBuilder->fireEvent(new AfterCollectingFieldRules($field, $fieldRules)); |
307
|
10 |
|
|
308
|
|
|
return $fieldRules; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param FormField[] $fields |
313
|
|
|
* @return array |
314
|
10 |
|
*/ |
315
|
|
|
public function mergeFieldsRules($fields) |
316
|
10 |
|
{ |
317
|
|
|
$rules = new Rules([]); |
318
|
10 |
|
|
319
|
10 |
|
foreach ($fields as $field) { |
320
|
|
|
$rules->append($this->getFieldValidationRules($field)); |
|
|
|
|
321
|
|
|
} |
322
|
10 |
|
|
323
|
|
|
return $rules; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param array $fields |
328
|
|
|
* @return array |
329
|
3 |
|
*/ |
330
|
|
|
public function mergeAttributes(array $fields) |
331
|
3 |
|
{ |
332
|
3 |
|
$attributes = []; |
333
|
3 |
|
foreach ($fields as $field) { |
334
|
|
|
$attributes = array_merge($attributes, $field->getAllAttributes()); |
335
|
|
|
} |
336
|
3 |
|
|
337
|
|
|
return $attributes; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Get a form's checkbox fields' names. |
342
|
|
|
* |
343
|
|
|
* @param Form $form |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
3 |
|
public function getBoolableFields(Form $form) |
347
|
|
|
{ |
348
|
|
|
$fields = []; |
349
|
3 |
|
foreach ($form->getFields() as $name => $field) { |
350
|
3 |
|
if ($field instanceof CheckableType && $field->getOption('value') == CheckableType::DEFAULT_VALUE) { |
351
|
2 |
|
$fields[] = $this->transformToDotSyntax($name); |
352
|
|
|
} |
353
|
2 |
|
} |
354
|
2 |
|
|
355
|
3 |
|
return $fields; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Turn checkbox fields into bools. |
360
|
3 |
|
* |
361
|
3 |
|
* @param Form $form |
362
|
|
|
* @param array $values |
363
|
|
|
* @return void |
364
|
|
|
*/ |
365
|
|
|
public function alterFieldValuesBools(Form $form, array &$values) |
366
|
|
|
{ |
367
|
|
|
$fields = $this->getBoolableFields($form); |
368
|
10 |
|
|
369
|
|
|
foreach ($fields as $name) { |
370
|
|
|
$value = Arr::get($values, $name, -1); |
371
|
10 |
|
if ($value !== -1) { |
372
|
|
|
Arr::set($values, $name, (int) (bool) $value); |
373
|
|
|
} |
374
|
10 |
|
} |
375
|
1 |
|
} |
376
|
1 |
|
|
377
|
|
|
/** |
378
|
|
|
* Alter a form's values recursively according to its fields. |
379
|
|
|
* |
380
|
10 |
|
* @param Form $form |
381
|
10 |
|
* @param array $values |
382
|
10 |
|
* @return void |
383
|
|
|
*/ |
384
|
|
|
public function alterFieldValues(Form $form, array &$values) |
385
|
10 |
|
{ |
386
|
|
|
$this->alterFieldValuesBools($form, $values); |
387
|
|
|
|
388
|
|
|
// Alter the form's child forms recursively |
389
|
|
|
foreach ($form->getFields() as $name => $field) { |
390
|
|
|
if (method_exists($field, 'alterFieldValues')) { |
391
|
|
|
$fullName = $this->transformToDotSyntax($name); |
392
|
1 |
|
|
393
|
|
|
$subValues = (array) Arr::get($values, $fullName); |
394
|
1 |
|
$field->alterFieldValues($subValues); |
|
|
|
|
395
|
1 |
|
Arr::set($values, $fullName, $subValues); |
396
|
1 |
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
1 |
|
// Alter the form itself |
400
|
1 |
|
$form->alterFieldValues($values); |
401
|
|
|
} |
402
|
|
|
|
403
|
1 |
|
/** |
404
|
|
|
* Alter a form's validity recursively, and add messages with nested form prefix. |
405
|
|
|
* |
406
|
|
|
* @return void |
407
|
|
|
*/ |
408
|
|
|
public function alterValid(Form $form, Form $mainForm, &$isValid) |
409
|
106 |
|
{ |
410
|
|
|
// Alter the form itself |
411
|
106 |
|
$messages = $form->alterValid($mainForm, $isValid); |
412
|
|
|
|
413
|
|
|
// Add messages to the existing Bag |
414
|
|
|
if ($messages) { |
415
|
|
|
$messageBag = $mainForm->getValidator()->getMessageBag(); |
416
|
|
|
$this->appendMessagesWithPrefix($messageBag, $form->getName(), $messages); |
417
|
|
|
} |
418
|
8 |
|
|
419
|
|
|
// Alter the form's child forms recursively |
420
|
8 |
|
foreach ($form->getFields() as $name => $field) { |
421
|
8 |
|
if (method_exists($field, 'alterValid')) { |
422
|
1 |
|
$field->alterValid($mainForm, $isValid); |
|
|
|
|
423
|
|
|
} |
424
|
|
|
} |
425
|
7 |
|
} |
426
|
7 |
|
|
427
|
|
|
/** |
428
|
|
|
* Add unprefixed messages with prefix to a MessageBag. |
429
|
|
|
* |
430
|
|
|
* @return void |
431
|
|
|
*/ |
432
|
3 |
|
public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
433
|
|
|
{ |
434
|
3 |
|
foreach ($keyedMessages as $key => $messages) { |
435
|
|
|
if ($prefix) { |
436
|
|
|
$key = $this->transformToDotSyntax($prefix . '[' . $key . ']'); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
foreach ((array) $messages as $message) { |
440
|
|
|
$messageBag->add($key, $message); |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
} |
444
|
74 |
|
|
445
|
|
|
/** |
446
|
74 |
|
* @param string $string |
447
|
2 |
|
* @return string |
448
|
2 |
|
*/ |
449
|
|
|
public function transformToDotSyntax($string) |
450
|
|
|
{ |
451
|
|
|
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $string); |
452
|
72 |
|
} |
453
|
2 |
|
|
454
|
2 |
|
/** |
455
|
2 |
|
* @param string $string |
456
|
|
|
* @return string |
457
|
|
|
*/ |
458
|
|
|
public function transformToBracketSyntax($string) |
459
|
70 |
|
{ |
460
|
|
|
$name = explode('.', $string); |
461
|
|
|
if ($name && count($name) == 1) { |
|
|
|
|
462
|
|
|
return $name[0]; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$first = array_shift($name); |
466
|
|
|
return $first . '[' . implode('][', $name) . ']'; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @return TranslatorInterface |
471
|
|
|
*/ |
472
|
|
|
public function getTranslator() |
473
|
|
|
{ |
474
|
|
|
return $this->translator; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Check if field name is valid and not reserved. |
479
|
|
|
* |
480
|
|
|
* @throws \InvalidArgumentException |
481
|
|
|
* @param string $name |
482
|
|
|
* @param string $className |
483
|
|
|
*/ |
484
|
|
|
public function checkFieldName($name, $className) |
485
|
|
|
{ |
486
|
|
|
if (!$name || trim($name) == '') { |
487
|
|
|
throw new \InvalidArgumentException( |
488
|
|
|
"Please provide valid field name for class [{$className}]" |
489
|
|
|
); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
if (in_array($name, static::$reservedFieldNames)) { |
493
|
|
|
throw new \InvalidArgumentException( |
494
|
|
|
"Field name [{$name}] in form [{$className}] is a reserved word. Please use a different field name." . |
495
|
|
|
"\nList of all reserved words: " . join(', ', static::$reservedFieldNames) |
496
|
|
|
); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
return true; |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..