1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; |
6
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidatorFactory; |
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
8
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use Kris\LaravelFormBuilder\Events\AfterFieldCreation; |
12
|
|
|
use Kris\LaravelFormBuilder\Events\AfterFormValidation; |
13
|
|
|
use Kris\LaravelFormBuilder\Events\BeforeFormValidation; |
14
|
|
|
use Kris\LaravelFormBuilder\Fields\FormField; |
15
|
|
|
use Kris\LaravelFormBuilder\Filters\FilterResolver; |
16
|
|
|
|
17
|
|
|
class Form |
18
|
|
|
{ |
19
|
|
|
// Simple fields |
20
|
|
|
const FIELD_TEXT = 'text'; |
21
|
|
|
const FIELD_TEXTAREA = 'textarea'; |
22
|
|
|
const FIELD_SELECT = 'select'; |
23
|
|
|
const FIELD_CHOICE = 'choice'; |
24
|
|
|
const FIELD_CHECKBOX = 'checkbox'; |
25
|
|
|
const FIELD_RADIO = 'radio'; |
26
|
|
|
const FIELD_PASSWORD = 'password'; |
27
|
|
|
const FIELD_HIDDEN = 'hidden'; |
28
|
|
|
const FIELD_FILE = 'file'; |
29
|
|
|
const FIELD_STATIC = 'static'; |
30
|
|
|
//Date time fields |
31
|
|
|
const FIELD_DATE = 'date'; |
32
|
|
|
const FIELD_DATETIME_LOCAL = 'datetime_local'; |
33
|
|
|
const FIELD_MONTH = 'month'; |
34
|
|
|
const FIELD_TIME = 'time'; |
35
|
|
|
const FIELD_WEEK = 'week'; |
36
|
|
|
//Special Purpose fields |
37
|
|
|
const FIELD_COLOR = 'color'; |
38
|
|
|
const FIELD_SEARCH = 'search'; |
39
|
|
|
const FIELD_IMAGE = 'image'; |
40
|
|
|
const FIELD_EMAIL = 'email'; |
41
|
|
|
const FIELD_URL = 'url'; |
42
|
|
|
const FIELD_TEL = 'tel'; |
43
|
|
|
const FIELD_NUMBER = 'number'; |
44
|
|
|
const FIELD_RANGE = 'range'; |
45
|
|
|
//Buttons |
46
|
|
|
const BUTTON_SUBMIT = 'submit'; |
47
|
|
|
const BUTTON_RESET = 'reset'; |
48
|
|
|
const BUTTON_BUTTON = 'button'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* All fields that are added. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $fields = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Model to use. |
59
|
|
|
* |
60
|
|
|
* @var mixed |
61
|
|
|
*/ |
62
|
|
|
protected $model = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var EventDispatcher |
66
|
|
|
*/ |
67
|
|
|
protected $eventDispatcher; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var FormHelper |
71
|
|
|
*/ |
72
|
|
|
protected $formHelper; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Form options. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $formOptions = [ |
80
|
|
|
'method' => 'GET', |
81
|
|
|
'url' => null |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Form specific configuration. |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $formConfig = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Additional data which can be used to build fields. |
93
|
|
|
* |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected $data = []; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Wether errors for each field should be shown when calling form($form) or form_rest($form). |
100
|
|
|
* |
101
|
|
|
* @var bool |
102
|
|
|
*/ |
103
|
|
|
protected $showFieldErrors = true; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Enable html5 validation. |
107
|
|
|
* |
108
|
|
|
* @var bool |
109
|
|
|
*/ |
110
|
|
|
protected $clientValidationEnabled = true; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Name of the parent form if any. |
114
|
|
|
* |
115
|
|
|
* @var string|null |
116
|
|
|
*/ |
117
|
|
|
protected $name = null; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var FormBuilder |
121
|
|
|
*/ |
122
|
|
|
protected $formBuilder; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var ValidatorFactory |
126
|
|
|
*/ |
127
|
|
|
protected $validatorFactory; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var Validator |
131
|
|
|
*/ |
132
|
|
|
protected $validator = null; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var Request |
136
|
|
|
*/ |
137
|
|
|
protected $request; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* List of fields to not render. |
141
|
|
|
* |
142
|
|
|
* @var array |
143
|
|
|
**/ |
144
|
|
|
protected $exclude = []; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Wether the form is beign rebuild. |
148
|
|
|
* |
149
|
|
|
* @var bool |
150
|
|
|
*/ |
151
|
|
|
protected $rebuilding = false; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @var string |
155
|
|
|
*/ |
156
|
|
|
protected $templatePrefix; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @var string |
160
|
|
|
*/ |
161
|
|
|
protected $languageName; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @var string |
165
|
|
|
*/ |
166
|
|
|
protected $translationTemplate; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* To filter and mutate request values or not. |
170
|
|
|
* |
171
|
|
|
* @var bool |
172
|
|
|
*/ |
173
|
|
|
protected $lockFiltering = false; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Define the error bag name for the form. |
177
|
|
|
* |
178
|
|
|
* @var string |
179
|
|
|
*/ |
180
|
|
|
protected $errorBag = 'default'; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Build the form. |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
3 |
|
public function buildForm() |
188
|
|
|
{ |
189
|
3 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Rebuild the form from scratch. |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
19 |
|
public function rebuildForm() |
197
|
|
|
{ |
198
|
19 |
|
$this->rebuilding = true; |
199
|
|
|
// If form is plain, buildForm method is empty, so we need to take |
200
|
|
|
// existing fields and add them again |
201
|
19 |
|
if (get_class($this) === 'Kris\LaravelFormBuilder\Form') { |
202
|
18 |
|
foreach ($this->fields as $name => $field) { |
203
|
|
|
// Remove any temp variables added in previous instance |
204
|
7 |
|
$options = array_except($field->getOptions(), 'tmp'); |
205
|
18 |
|
$this->add($name, $field->getType(), $options); |
206
|
|
|
} |
207
|
|
|
} else { |
208
|
3 |
|
$this->buildForm(); |
209
|
|
|
} |
210
|
19 |
|
$this->rebuilding = false; |
211
|
|
|
|
212
|
19 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Create the FormField object. |
217
|
|
|
* |
218
|
|
|
* @param string $name |
219
|
|
|
* @param string $type |
220
|
|
|
* @param array $options |
221
|
|
|
* @return FormField |
222
|
|
|
*/ |
223
|
65 |
|
protected function makeField($name, $type = 'text', array $options = []) |
224
|
|
|
{ |
225
|
65 |
|
$this->setupFieldOptions($name, $options); |
226
|
|
|
|
227
|
65 |
|
$fieldName = $this->getFieldName($name); |
228
|
|
|
|
229
|
65 |
|
$fieldType = $this->getFieldType($type); |
230
|
|
|
|
231
|
64 |
|
$field = new $fieldType($fieldName, $type, $this, $options); |
232
|
|
|
|
233
|
61 |
|
$this->eventDispatcher->fire(new AfterFieldCreation($this, $field)); |
234
|
|
|
|
235
|
61 |
|
return $field; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Create a new field and add it to the form. |
240
|
|
|
* |
241
|
|
|
* @param string $name |
242
|
|
|
* @param string $type |
243
|
|
|
* @param array $options |
244
|
|
|
* @param bool $modify |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
67 |
|
public function add($name, $type = 'text', array $options = [], $modify = false) |
248
|
|
|
{ |
249
|
67 |
|
$this->formHelper->checkFieldName($name, get_class($this)); |
250
|
|
|
|
251
|
65 |
|
if ($this->rebuilding && !$this->has($name)) { |
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
65 |
|
$this->addField($this->makeField($name, $type, $options), $modify); |
256
|
|
|
|
257
|
61 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Add a FormField to the form's fields. |
262
|
|
|
* |
263
|
|
|
* @param FormField $field |
264
|
|
|
* @return $this |
265
|
|
|
*/ |
266
|
61 |
|
protected function addField(FormField $field, $modify = false) |
267
|
|
|
{ |
268
|
61 |
|
if (!$modify && !$this->rebuilding) { |
269
|
61 |
|
$this->preventDuplicate($field->getRealName()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
61 |
|
if ($field->getType() == 'file') { |
274
|
3 |
|
$this->formOptions['files'] = true; |
275
|
|
|
} |
276
|
|
|
|
277
|
61 |
|
$this->fields[$field->getRealName()] = $field; |
278
|
|
|
|
279
|
61 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Add field before another field. |
284
|
|
|
* |
285
|
|
|
* @param string $name Name of the field before which new field is added. |
286
|
|
|
* @param string $fieldName Field name which will be added. |
287
|
|
|
* @param string $type |
288
|
|
|
* @param array $options |
289
|
|
|
* @param bool $modify |
290
|
|
|
* @return $this |
291
|
|
|
*/ |
292
|
1 |
|
public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
293
|
|
|
{ |
294
|
1 |
|
$offset = array_search($name, array_keys($this->fields)); |
295
|
|
|
|
296
|
1 |
|
$beforeFields = array_slice($this->fields, 0, $offset); |
297
|
1 |
|
$afterFields = array_slice($this->fields, $offset); |
298
|
|
|
|
299
|
1 |
|
$this->fields = $beforeFields; |
300
|
|
|
|
301
|
1 |
|
$this->add($fieldName, $type, $options, $modify); |
302
|
|
|
|
303
|
1 |
|
$this->fields += $afterFields; |
304
|
|
|
|
305
|
1 |
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Add field before another field. |
310
|
|
|
* |
311
|
|
|
* @param string $name Name of the field after which new field is added. |
312
|
|
|
* @param string $fieldName Field name which will be added. |
313
|
|
|
* @param string $type |
314
|
|
|
* @param array $options |
315
|
|
|
* @param bool $modify |
316
|
|
|
* @return $this |
317
|
|
|
*/ |
318
|
1 |
|
public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
319
|
|
|
{ |
320
|
1 |
|
$offset = array_search($name, array_keys($this->fields)); |
321
|
|
|
|
322
|
1 |
|
$beforeFields = array_slice($this->fields, 0, $offset + 1); |
323
|
1 |
|
$afterFields = array_slice($this->fields, $offset + 1); |
324
|
|
|
|
325
|
1 |
|
$this->fields = $beforeFields; |
326
|
|
|
|
327
|
1 |
|
$this->add($fieldName, $type, $options, $modify); |
328
|
|
|
|
329
|
1 |
|
$this->fields += $afterFields; |
330
|
|
|
|
331
|
1 |
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Take another form and add it's fields directly to this form. |
336
|
|
|
* |
337
|
|
|
* @param mixed $class Form to merge. |
338
|
|
|
* @param array $options |
339
|
|
|
* @param boolean $modify |
340
|
|
|
* @return $this |
341
|
|
|
*/ |
342
|
1 |
|
public function compose($class, array $options = [], $modify = false) |
343
|
|
|
{ |
344
|
1 |
|
$options['class'] = $class; |
345
|
|
|
|
346
|
|
|
// If we pass a ready made form just extract the fields. |
347
|
1 |
|
if ($class instanceof Form) { |
348
|
1 |
|
$fields = $class->getFields(); |
349
|
|
|
} elseif ($class instanceof Fields\ChildFormType) { |
350
|
|
|
$fields = $class->getForm()->getFields(); |
351
|
|
|
} elseif (is_string($class)) { |
352
|
|
|
// If its a string of a class make it the usual way. |
353
|
|
|
$options['model'] = $this->model; |
354
|
|
|
$options['name'] = $this->name; |
355
|
|
|
|
356
|
|
|
$form = $this->formBuilder->create($class, $options); |
357
|
|
|
$fields = $form->getFields(); |
358
|
|
|
} else { |
359
|
|
|
throw new \InvalidArgumentException( |
360
|
|
|
"[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType" |
361
|
|
|
); |
362
|
|
|
} |
363
|
|
|
|
364
|
1 |
|
foreach ($fields as $field) { |
365
|
1 |
|
$this->addField($field, $modify); |
366
|
|
|
} |
367
|
|
|
|
368
|
1 |
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Remove field with specified name from the form. |
373
|
|
|
* |
374
|
|
|
* @param $name |
375
|
|
|
* @return $this |
376
|
|
|
*/ |
377
|
2 |
|
public function remove($name) |
378
|
|
|
{ |
379
|
2 |
|
if ($this->has($name)) { |
380
|
2 |
|
unset($this->fields[$name]); |
381
|
|
|
} |
382
|
|
|
|
383
|
2 |
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Modify existing field. If it doesn't exist, it is added to form. |
388
|
|
|
* |
389
|
|
|
* @param string $name |
390
|
|
|
* @param string $type |
391
|
|
|
* @param array $options |
392
|
|
|
* @param bool $overwriteOptions |
393
|
|
|
* @return Form |
394
|
|
|
*/ |
395
|
1 |
|
public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
396
|
|
|
{ |
397
|
|
|
// If we don't want to overwrite options, we merge them with old options. |
398
|
1 |
|
if ($overwriteOptions === false && $this->has($name)) { |
399
|
1 |
|
$options = $this->formHelper->mergeOptions( |
400
|
1 |
|
$this->getField($name)->getOptions(), |
401
|
1 |
|
$options |
402
|
|
|
); |
403
|
|
|
} |
404
|
|
|
|
405
|
1 |
|
return $this->add($name, $type, $options, true); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Render full form. |
410
|
|
|
* |
411
|
|
|
* @param array $options |
412
|
|
|
* @param bool $showStart |
413
|
|
|
* @param bool $showFields |
414
|
|
|
* @param bool $showEnd |
415
|
|
|
* @return string |
416
|
|
|
*/ |
417
|
7 |
|
public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
418
|
|
|
{ |
419
|
7 |
|
return $this->render($options, $this->fields, $showStart, $showFields, $showEnd); |
|
|
|
|
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Render rest of the form. |
424
|
|
|
* |
425
|
|
|
* @param bool $showFormEnd |
426
|
|
|
* @param bool $showFields |
427
|
|
|
* @return string |
428
|
|
|
*/ |
429
|
1 |
|
public function renderRest($showFormEnd = true, $showFields = true) |
430
|
|
|
{ |
431
|
1 |
|
$fields = $this->getUnrenderedFields(); |
432
|
|
|
|
433
|
1 |
|
return $this->render([], $fields, false, $showFields, $showFormEnd); |
|
|
|
|
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Renders the rest of the form up until the specified field name. |
438
|
|
|
* |
439
|
|
|
* @param string $field_name |
440
|
|
|
* @param bool $showFormEnd |
441
|
|
|
* @param bool $showFields |
442
|
|
|
* @return string |
443
|
|
|
*/ |
444
|
2 |
|
public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
445
|
|
|
{ |
446
|
2 |
|
if (!$this->has($field_name)) { |
447
|
1 |
|
$this->fieldDoesNotExist($field_name); |
448
|
|
|
} |
449
|
|
|
|
450
|
1 |
|
$fields = $this->getUnrenderedFields(); |
451
|
|
|
|
452
|
1 |
|
$i = 1; |
453
|
1 |
|
foreach ($fields as $key => $value) { |
454
|
1 |
|
if ($value->getRealName() == $field_name) { |
455
|
1 |
|
break; |
456
|
|
|
} |
457
|
1 |
|
$i++; |
458
|
|
|
} |
459
|
|
|
|
460
|
1 |
|
$fields = array_slice($fields, 0, $i, true); |
461
|
|
|
|
462
|
1 |
|
return $this->render([], $fields, false, $showFields, $showFormEnd); |
|
|
|
|
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Get single field instance from form object. |
467
|
|
|
* |
468
|
|
|
* @param string $name |
469
|
|
|
* @return FormField |
470
|
|
|
*/ |
471
|
34 |
|
public function getField($name) |
472
|
|
|
{ |
473
|
34 |
|
if ($this->has($name)) { |
474
|
33 |
|
return $this->fields[$name]; |
475
|
|
|
} |
476
|
|
|
|
477
|
1 |
|
$this->fieldDoesNotExist($name); |
478
|
|
|
} |
479
|
|
|
|
480
|
101 |
|
public function getErrorBag() |
481
|
|
|
{ |
482
|
101 |
|
return $this->errorBag; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Check if form has field. |
487
|
|
|
* |
488
|
|
|
* @param string $name |
489
|
|
|
* @return bool |
490
|
|
|
*/ |
491
|
61 |
|
public function has($name) |
492
|
|
|
{ |
493
|
61 |
|
return array_key_exists($name, $this->fields); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Get all form options. |
498
|
|
|
* |
499
|
|
|
* @return array |
500
|
|
|
*/ |
501
|
2 |
|
public function getFormOptions() |
502
|
|
|
{ |
503
|
2 |
|
return $this->formOptions; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Get single form option. |
508
|
|
|
* |
509
|
|
|
* @param string $option |
510
|
|
|
* @param mixed|null $default |
511
|
|
|
* @return mixed |
512
|
|
|
*/ |
513
|
129 |
|
public function getFormOption($option, $default = null) |
514
|
|
|
{ |
515
|
129 |
|
return array_get($this->formOptions, $option, $default); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Set single form option on form. |
520
|
|
|
* |
521
|
|
|
* @param string $option |
522
|
|
|
* @param mixed $value |
523
|
|
|
* |
524
|
|
|
* @return $this |
525
|
|
|
*/ |
526
|
2 |
|
public function setFormOption($option, $value) |
527
|
|
|
{ |
528
|
2 |
|
$this->formOptions[$option] = $value; |
529
|
|
|
|
530
|
2 |
|
return $this; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Get the passed config key using the custom |
535
|
|
|
* form config, if any. |
536
|
|
|
* |
537
|
|
|
* @param string $key |
538
|
|
|
* @param mixed $default |
539
|
|
|
* |
540
|
|
|
* @return mixed |
541
|
|
|
*/ |
542
|
103 |
|
public function getConfig($key = null, $default = null) |
543
|
|
|
{ |
544
|
103 |
|
return $this->formHelper->getConfig($key, $default, $this->formConfig); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Set form options. |
549
|
|
|
* |
550
|
|
|
* @param array $formOptions |
551
|
|
|
* @return $this |
552
|
|
|
*/ |
553
|
129 |
|
public function setFormOptions(array $formOptions) |
554
|
|
|
{ |
555
|
129 |
|
$this->formOptions = $this->formHelper->mergeOptions($this->formOptions, $formOptions); |
556
|
129 |
|
$this->checkIfNamedForm(); |
557
|
129 |
|
$this->pullFromOptions('data', 'addData'); |
558
|
129 |
|
$this->pullFromOptions('model', 'setupModel'); |
559
|
129 |
|
$this->pullFromOptions('errors_enabled', 'setErrorsEnabled'); |
560
|
129 |
|
$this->pullFromOptions('client_validation', 'setClientValidationEnabled'); |
561
|
129 |
|
$this->pullFromOptions('template_prefix', 'setTemplatePrefix'); |
562
|
129 |
|
$this->pullFromOptions('language_name', 'setLanguageName'); |
563
|
129 |
|
$this->pullFromOptions('translation_template', 'setTranslationTemplate'); |
564
|
|
|
|
565
|
129 |
|
return $this; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Get an option from provided options and call method with that value. |
570
|
|
|
* |
571
|
|
|
* @param string $name |
572
|
|
|
* @param string $method |
573
|
|
|
*/ |
574
|
129 |
|
protected function pullFromOptions($name, $method) |
575
|
|
|
{ |
576
|
129 |
|
if (array_get($this->formOptions, $name) !== null) { |
577
|
20 |
|
$this->{$method}(array_pull($this->formOptions, $name)); |
578
|
|
|
} |
579
|
129 |
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Get form http method. |
583
|
|
|
* |
584
|
|
|
* @return string |
585
|
|
|
*/ |
586
|
3 |
|
public function getMethod() |
587
|
|
|
{ |
588
|
3 |
|
return $this->formOptions['method']; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Set form http method. |
593
|
|
|
* |
594
|
|
|
* @param string $method |
595
|
|
|
* @return $this |
596
|
|
|
*/ |
597
|
1 |
|
public function setMethod($method) |
598
|
|
|
{ |
599
|
1 |
|
$this->formOptions['method'] = $method; |
600
|
|
|
|
601
|
1 |
|
return $this; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Get form action url. |
606
|
|
|
* |
607
|
|
|
* @return string |
608
|
|
|
*/ |
609
|
3 |
|
public function getUrl() |
610
|
|
|
{ |
611
|
3 |
|
return $this->formOptions['url']; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Set form action url. |
616
|
|
|
* |
617
|
|
|
* @param string $url |
618
|
|
|
* @return $this |
619
|
|
|
*/ |
620
|
1 |
|
public function setUrl($url) |
621
|
|
|
{ |
622
|
1 |
|
$this->formOptions['url'] = $url; |
623
|
|
|
|
624
|
1 |
|
return $this; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Returns the name of the form. |
629
|
|
|
* |
630
|
|
|
* @return string|null |
631
|
|
|
*/ |
632
|
69 |
|
public function getName() |
633
|
|
|
{ |
634
|
69 |
|
return $this->name; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Set the name of the form. |
639
|
|
|
* |
640
|
|
|
* @param string $name |
641
|
|
|
* @param bool $rebuild |
642
|
|
|
* @return $this |
643
|
|
|
*/ |
644
|
12 |
|
public function setName($name, $rebuild = true) |
645
|
|
|
{ |
646
|
12 |
|
$this->name = $name; |
647
|
|
|
|
648
|
12 |
|
if ($rebuild) { |
649
|
12 |
|
$this->rebuildForm(); |
650
|
|
|
} |
651
|
|
|
|
652
|
12 |
|
return $this; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Get model that is bind to form object. |
657
|
|
|
* |
658
|
|
|
* @return mixed |
659
|
|
|
*/ |
660
|
96 |
|
public function getModel() |
661
|
|
|
{ |
662
|
96 |
|
return $this->model; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Set model to form object. |
667
|
|
|
* |
668
|
|
|
* @param mixed $model |
669
|
|
|
* @return $this |
670
|
|
|
* @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
671
|
|
|
*/ |
672
|
17 |
|
public function setModel($model) |
673
|
|
|
{ |
674
|
17 |
|
$this->model = $model; |
675
|
|
|
|
676
|
17 |
|
$this->rebuildForm(); |
677
|
|
|
|
678
|
17 |
|
return $this; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Setup model for form, add namespace if needed for child forms. |
683
|
|
|
* |
684
|
|
|
* @return $this |
685
|
|
|
*/ |
686
|
12 |
|
protected function setupModel($model) |
687
|
|
|
{ |
688
|
12 |
|
$this->model = $model; |
689
|
12 |
|
$this->setupNamedModel(); |
690
|
|
|
|
691
|
12 |
|
return $this; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Get all fields. |
696
|
|
|
* |
697
|
|
|
* @return FormField[] |
698
|
|
|
*/ |
699
|
129 |
|
public function getFields() |
700
|
|
|
{ |
701
|
129 |
|
return $this->fields; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Get field dynamically. |
706
|
|
|
* |
707
|
|
|
* @param string $name |
708
|
|
|
* @return FormField |
709
|
|
|
*/ |
710
|
20 |
|
public function __get($name) |
711
|
|
|
{ |
712
|
20 |
|
if ($this->has($name)) { |
713
|
19 |
|
return $this->getField($name); |
714
|
|
|
} |
715
|
3 |
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Check if field exists when fetched using magic methods. |
719
|
|
|
* |
720
|
|
|
* @param string $name |
721
|
|
|
* @return bool |
722
|
|
|
*/ |
723
|
|
|
public function __isset($name) |
724
|
|
|
{ |
725
|
|
|
return $this->has($name); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Set the Event Dispatcher to fire Laravel events. |
730
|
|
|
* |
731
|
|
|
* @param EventDispatcher $eventDispatcher |
732
|
|
|
* @return $this |
733
|
|
|
*/ |
734
|
129 |
|
public function setEventDispatcher(EventDispatcher $eventDispatcher) |
735
|
|
|
{ |
736
|
129 |
|
$this->eventDispatcher = $eventDispatcher; |
737
|
|
|
|
738
|
129 |
|
return $this; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* Set the form helper only on first instantiation. |
743
|
|
|
* |
744
|
|
|
* @param FormHelper $formHelper |
745
|
|
|
* @return $this |
746
|
|
|
*/ |
747
|
129 |
|
public function setFormHelper(FormHelper $formHelper) |
748
|
|
|
{ |
749
|
129 |
|
$this->formHelper = $formHelper; |
750
|
|
|
|
751
|
129 |
|
return $this; |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Get form helper. |
756
|
|
|
* |
757
|
|
|
* @return FormHelper |
758
|
|
|
*/ |
759
|
101 |
|
public function getFormHelper() |
760
|
|
|
{ |
761
|
101 |
|
return $this->formHelper; |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Add custom field. |
766
|
|
|
* |
767
|
|
|
* @param $name |
768
|
|
|
* @param $class |
769
|
|
|
*/ |
770
|
2 |
|
public function addCustomField($name, $class) |
771
|
|
|
{ |
772
|
2 |
|
if ($this->rebuilding && $this->formHelper->hasCustomField($name)) { |
773
|
|
|
return $this; |
774
|
|
|
} |
775
|
|
|
|
776
|
2 |
|
$this->formHelper->addCustomField($name, $class); |
777
|
2 |
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Returns wether form errors should be shown under every field. |
781
|
|
|
* |
782
|
|
|
* @return bool |
783
|
|
|
*/ |
784
|
101 |
|
public function haveErrorsEnabled() |
785
|
|
|
{ |
786
|
101 |
|
return $this->showFieldErrors; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* Enable or disable showing errors under fields |
791
|
|
|
* |
792
|
|
|
* @param bool $enabled |
793
|
|
|
* @return $this |
794
|
|
|
*/ |
795
|
1 |
|
public function setErrorsEnabled($enabled) |
796
|
|
|
{ |
797
|
1 |
|
$this->showFieldErrors = (bool) $enabled; |
798
|
|
|
|
799
|
1 |
|
return $this; |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
/** |
803
|
|
|
* Is client validation enabled? |
804
|
|
|
* |
805
|
|
|
* @return bool |
806
|
|
|
*/ |
807
|
101 |
|
public function clientValidationEnabled() |
808
|
|
|
{ |
809
|
101 |
|
return $this->clientValidationEnabled; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Enable/disable client validation. |
814
|
|
|
* |
815
|
|
|
* @param bool $enable |
816
|
|
|
* @return $this |
817
|
|
|
*/ |
818
|
2 |
|
public function setClientValidationEnabled($enable) |
819
|
|
|
{ |
820
|
2 |
|
$this->clientValidationEnabled = (bool) $enable; |
821
|
|
|
|
822
|
2 |
|
return $this; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* Add any aditional data that field needs (ex. array of choices). |
827
|
|
|
* |
828
|
|
|
* @deprecated deprecated since 1.6.20, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data |
829
|
|
|
* will be switched to protected in 1.7. |
830
|
|
|
* @param string $name |
831
|
|
|
* @param mixed $data |
832
|
|
|
*/ |
833
|
1 |
|
public function setData($name, $data) |
834
|
|
|
{ |
835
|
1 |
|
$this->data[$name] = $data; |
836
|
1 |
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* Get single additional data. |
840
|
|
|
* |
841
|
|
|
* @param string $name |
842
|
|
|
* @param null $default |
843
|
|
|
* @return mixed |
844
|
|
|
*/ |
845
|
20 |
|
public function getData($name = null, $default = null) |
846
|
|
|
{ |
847
|
20 |
|
if (is_null($name)) { |
848
|
19 |
|
return $this->data; |
849
|
|
|
} |
850
|
|
|
|
851
|
1 |
|
return array_get($this->data, $name, $default); |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* Add multiple peices of data at once. |
856
|
|
|
* |
857
|
|
|
* @deprecated deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data |
858
|
|
|
* will be switched to protected in 1.7. |
859
|
|
|
* @param $data |
860
|
|
|
* @return $this |
861
|
|
|
**/ |
862
|
129 |
|
public function addData(array $data) |
863
|
|
|
{ |
864
|
129 |
|
foreach ($data as $key => $value) { |
865
|
1 |
|
$this->setData($key, $value); |
|
|
|
|
866
|
|
|
} |
867
|
|
|
|
868
|
129 |
|
return $this; |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* Get current request. |
873
|
|
|
* |
874
|
|
|
* @return \Illuminate\Http\Request |
875
|
|
|
*/ |
876
|
129 |
|
public function getRequest() |
877
|
|
|
{ |
878
|
129 |
|
return $this->request; |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
/** |
882
|
|
|
* Set request on form. |
883
|
|
|
* |
884
|
|
|
* @param Request $request |
885
|
|
|
* @return $this |
886
|
|
|
*/ |
887
|
129 |
|
public function setRequest(Request $request) |
888
|
|
|
{ |
889
|
129 |
|
$this->request = $request; |
890
|
|
|
|
891
|
129 |
|
return $this; |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* Get template prefix that is prepended to all template paths. |
896
|
|
|
* |
897
|
|
|
* @return string |
898
|
|
|
*/ |
899
|
39 |
|
public function getTemplatePrefix() |
900
|
|
|
{ |
901
|
39 |
|
if ($this->templatePrefix !== null) { |
902
|
4 |
|
return $this->templatePrefix; |
903
|
|
|
} |
904
|
|
|
|
905
|
35 |
|
return $this->getConfig('template_prefix'); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
/** |
909
|
|
|
* Set a template prefix for the form and its fields. |
910
|
|
|
* |
911
|
|
|
* @param string $prefix |
912
|
|
|
* @return $this |
913
|
|
|
*/ |
914
|
4 |
|
public function setTemplatePrefix($prefix) |
915
|
|
|
{ |
916
|
4 |
|
$this->templatePrefix = (string) $prefix; |
917
|
|
|
|
918
|
4 |
|
return $this; |
919
|
|
|
} |
920
|
|
|
|
921
|
|
|
/** |
922
|
|
|
* Get the language name. |
923
|
|
|
* |
924
|
|
|
* @return string |
925
|
|
|
*/ |
926
|
98 |
|
public function getLanguageName() |
927
|
|
|
{ |
928
|
98 |
|
return $this->languageName; |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
/** |
932
|
|
|
* Set a language name, used as prefix for translated strings. |
933
|
|
|
* |
934
|
|
|
* @param string $prefix |
935
|
|
|
* @return $this |
936
|
|
|
*/ |
937
|
13 |
|
public function setLanguageName($prefix) |
938
|
|
|
{ |
939
|
13 |
|
$this->languageName = (string) $prefix; |
940
|
|
|
|
941
|
13 |
|
return $this; |
942
|
|
|
} |
943
|
|
|
|
944
|
|
|
/** |
945
|
|
|
* Get the translation template. |
946
|
|
|
* |
947
|
|
|
* @return string |
948
|
|
|
*/ |
949
|
100 |
|
public function getTranslationTemplate() |
950
|
|
|
{ |
951
|
100 |
|
return $this->translationTemplate; |
952
|
|
|
} |
953
|
|
|
|
954
|
|
|
/** |
955
|
|
|
* Set a translation template, used to determine labels for fields. |
956
|
|
|
* |
957
|
|
|
* @param string $template |
958
|
|
|
* @return $this |
959
|
|
|
*/ |
960
|
11 |
|
public function setTranslationTemplate($template) |
961
|
|
|
{ |
962
|
11 |
|
$this->translationTemplate = (string) $template; |
963
|
|
|
|
964
|
11 |
|
return $this; |
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
/** |
968
|
|
|
* Render the form. |
969
|
|
|
* |
970
|
|
|
* @param array $options |
971
|
|
|
* @param string $fields |
972
|
|
|
* @param bool $showStart |
973
|
|
|
* @param bool $showFields |
974
|
|
|
* @param bool $showEnd |
975
|
|
|
* @return string |
976
|
|
|
*/ |
977
|
9 |
|
protected function render($options, $fields, $showStart, $showFields, $showEnd) |
978
|
|
|
{ |
979
|
9 |
|
$formOptions = $this->formHelper->mergeOptions($this->formOptions, $options); |
980
|
|
|
|
981
|
9 |
|
$this->setupNamedModel(); |
982
|
|
|
|
983
|
9 |
|
return $this->formHelper->getView() |
984
|
9 |
|
->make($this->getTemplate()) |
985
|
9 |
|
->with(compact('showStart', 'showFields', 'showEnd')) |
986
|
9 |
|
->with('formOptions', $formOptions) |
987
|
9 |
|
->with('fields', $fields) |
988
|
9 |
|
->with('model', $this->getModel()) |
989
|
9 |
|
->with('exclude', $this->exclude) |
990
|
9 |
|
->with('form', $this) |
991
|
9 |
|
->render(); |
992
|
|
|
} |
993
|
|
|
|
994
|
|
|
/** |
995
|
|
|
* Get template from options if provided, otherwise fallback to config. |
996
|
|
|
* |
997
|
|
|
* @return mixed |
998
|
|
|
*/ |
999
|
9 |
|
protected function getTemplate() |
1000
|
|
|
{ |
1001
|
9 |
|
return $this->getTemplatePrefix() . $this->getFormOption('template', $this->getConfig('form')); |
1002
|
|
|
} |
1003
|
|
|
|
1004
|
|
|
/** |
1005
|
|
|
* Get all fields that are not rendered. |
1006
|
|
|
* |
1007
|
|
|
* @return array |
1008
|
|
|
*/ |
1009
|
2 |
|
protected function getUnrenderedFields() |
1010
|
|
|
{ |
1011
|
2 |
|
$unrenderedFields = []; |
1012
|
|
|
|
1013
|
2 |
|
foreach ($this->fields as $field) { |
1014
|
2 |
|
if (!$field->isRendered()) { |
1015
|
2 |
|
$unrenderedFields[] = $field; |
1016
|
2 |
|
continue; |
1017
|
|
|
} |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
2 |
|
return $unrenderedFields; |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
|
|
/** |
1024
|
|
|
* Prevent adding fields with same name. |
1025
|
|
|
* |
1026
|
|
|
* @param string $name |
1027
|
|
|
* @throws \InvalidArgumentException |
1028
|
|
|
* @return void |
1029
|
|
|
*/ |
1030
|
61 |
|
protected function preventDuplicate($name) |
1031
|
|
|
{ |
1032
|
61 |
|
if ($this->has($name)) { |
1033
|
1 |
|
throw new \InvalidArgumentException('Field ['.$name.'] already exists in the form '.get_class($this)); |
1034
|
|
|
} |
1035
|
61 |
|
} |
1036
|
|
|
|
1037
|
|
|
/** |
1038
|
|
|
* Returns and checks the type of the field. |
1039
|
|
|
* |
1040
|
|
|
* @param string $type |
1041
|
|
|
* @return string |
1042
|
|
|
*/ |
1043
|
65 |
|
protected function getFieldType($type) |
1044
|
|
|
{ |
1045
|
65 |
|
$fieldType = $this->formHelper->getFieldType($type); |
1046
|
|
|
|
1047
|
64 |
|
return $fieldType; |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
|
|
/** |
1051
|
|
|
* Check if form is named form. |
1052
|
|
|
* |
1053
|
|
|
* @return void |
1054
|
|
|
*/ |
1055
|
129 |
|
protected function checkIfNamedForm() |
1056
|
|
|
{ |
1057
|
129 |
|
if ($this->getFormOption('name')) { |
1058
|
8 |
|
$this->name = array_pull($this->formOptions, 'name', $this->name); |
1059
|
|
|
} |
1060
|
129 |
|
} |
1061
|
|
|
|
1062
|
|
|
/** |
1063
|
|
|
* Set up options on single field depending on form options. |
1064
|
|
|
* |
1065
|
|
|
* @param string $name |
1066
|
|
|
* @param $options |
1067
|
|
|
*/ |
1068
|
65 |
|
protected function setupFieldOptions($name, &$options) |
1069
|
|
|
{ |
1070
|
65 |
|
$options['real_name'] = $name; |
1071
|
65 |
|
} |
1072
|
|
|
|
1073
|
|
|
/** |
1074
|
|
|
* Set namespace to model if form is named so the data is bound properly. |
1075
|
|
|
* Returns true if model is changed, otherwise false. |
1076
|
|
|
* |
1077
|
|
|
* @return bool |
1078
|
|
|
*/ |
1079
|
21 |
|
protected function setupNamedModel() |
1080
|
|
|
{ |
1081
|
21 |
|
if (!$this->getModel() || !$this->getName()) { |
1082
|
19 |
|
return false; |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
3 |
|
$dotName = $this->formHelper->transformToDotSyntax($this->getName()); |
1086
|
3 |
|
$model = $this->formHelper->convertModelToArray($this->getModel()); |
1087
|
3 |
|
$isCollectionFormModel = preg_match('/^.*\.\d$/', $dotName); |
1088
|
3 |
|
$isCollectionPrototype = strpos($dotName, '__NAME__') !== false; |
1089
|
|
|
|
1090
|
3 |
|
if (!array_get($model, $dotName) && !$isCollectionFormModel && !$isCollectionPrototype) { |
|
|
|
|
1091
|
1 |
|
$newModel = []; |
1092
|
1 |
|
array_set($newModel, $dotName, $model); |
1093
|
1 |
|
$this->model = $newModel; |
1094
|
|
|
|
1095
|
1 |
|
return true; |
1096
|
|
|
} |
1097
|
|
|
|
1098
|
2 |
|
return false; |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* Set form builder instance on helper so we can use it later. |
1103
|
|
|
* |
1104
|
|
|
* @param FormBuilder $formBuilder |
1105
|
|
|
* @return $this |
1106
|
|
|
*/ |
1107
|
129 |
|
public function setFormBuilder(FormBuilder $formBuilder) |
1108
|
|
|
{ |
1109
|
129 |
|
$this->formBuilder = $formBuilder; |
1110
|
|
|
|
1111
|
129 |
|
return $this; |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
/** |
1115
|
|
|
* Returns the instance of the FormBuilder. |
1116
|
|
|
* |
1117
|
|
|
* @return FormBuilder |
1118
|
|
|
*/ |
1119
|
12 |
|
public function getFormBuilder() |
1120
|
|
|
{ |
1121
|
12 |
|
return $this->formBuilder; |
1122
|
|
|
} |
1123
|
|
|
|
1124
|
|
|
/** |
1125
|
|
|
* Set the Validator instance on this so we can use it later. |
1126
|
|
|
* |
1127
|
|
|
* @param ValidatorFactory $validator |
1128
|
|
|
* @return $this |
1129
|
|
|
*/ |
1130
|
129 |
|
public function setValidator(ValidatorFactory $validator) |
1131
|
|
|
{ |
1132
|
129 |
|
$this->validatorFactory = $validator; |
1133
|
|
|
|
1134
|
129 |
|
return $this; |
1135
|
|
|
} |
1136
|
|
|
|
1137
|
|
|
/** |
1138
|
|
|
* Returns the validator instance. |
1139
|
|
|
* |
1140
|
|
|
* @return Validator |
1141
|
|
|
*/ |
1142
|
1 |
|
public function getValidator() |
1143
|
|
|
{ |
1144
|
1 |
|
return $this->validator; |
1145
|
|
|
} |
1146
|
|
|
|
1147
|
|
|
/** |
1148
|
|
|
* Exclude some fields from rendering. |
1149
|
|
|
* |
1150
|
|
|
* @return $this |
1151
|
|
|
*/ |
1152
|
|
|
public function exclude(array $fields) |
1153
|
|
|
{ |
1154
|
|
|
$this->exclude = array_merge($this->exclude, $fields); |
1155
|
|
|
|
1156
|
|
|
return $this; |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
|
|
/** |
1160
|
|
|
* If form is named form, modify names to be contained in single key (parent[child_field_name]). |
1161
|
|
|
* |
1162
|
|
|
* @param string $name |
1163
|
|
|
* @return string |
1164
|
|
|
*/ |
1165
|
65 |
|
protected function getFieldName($name) |
1166
|
|
|
{ |
1167
|
65 |
|
$formName = $this->getName(); |
1168
|
65 |
|
if ($formName !== null) { |
1169
|
14 |
|
if (strpos($formName, '[') !== false || strpos($formName, ']') !== false) { |
1170
|
6 |
|
return $this->formHelper->transformToBracketSyntax( |
1171
|
6 |
|
$this->formHelper->transformToDotSyntax( |
1172
|
6 |
|
$formName . '[' . $name . ']' |
1173
|
|
|
) |
1174
|
|
|
); |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
11 |
|
return $formName . '[' . $name . ']'; |
1178
|
|
|
} |
1179
|
|
|
|
1180
|
65 |
|
return $name; |
1181
|
|
|
} |
1182
|
|
|
|
1183
|
|
|
/** |
1184
|
|
|
* Disable all fields in a form. |
1185
|
|
|
*/ |
1186
|
1 |
|
public function disableFields() |
1187
|
|
|
{ |
1188
|
1 |
|
foreach ($this->fields as $field) { |
1189
|
1 |
|
$field->disable(); |
1190
|
|
|
} |
1191
|
1 |
|
} |
1192
|
|
|
|
1193
|
|
|
/** |
1194
|
|
|
* Enable all fields in a form. |
1195
|
|
|
*/ |
1196
|
1 |
|
public function enableFields() |
1197
|
|
|
{ |
1198
|
1 |
|
foreach ($this->fields as $field) { |
1199
|
1 |
|
$field->enable(); |
1200
|
|
|
} |
1201
|
1 |
|
} |
1202
|
|
|
|
1203
|
|
|
/** |
1204
|
|
|
* Validate the form. |
1205
|
|
|
* |
1206
|
|
|
* @param array $validationRules |
1207
|
|
|
* @param array $messages |
1208
|
|
|
* @return Validator |
1209
|
|
|
*/ |
1210
|
9 |
|
public function validate($validationRules = [], $messages = []) |
1211
|
|
|
{ |
1212
|
9 |
|
$fieldRules = $this->formHelper->mergeFieldsRules($this->fields); |
1213
|
9 |
|
$rules = array_merge($fieldRules['rules'], $validationRules); |
1214
|
9 |
|
$messages = array_merge($fieldRules['error_messages'], $messages); |
1215
|
|
|
|
1216
|
9 |
|
$this->validator = $this->validatorFactory->make($this->getRequest()->all(), $rules, $messages); |
1217
|
9 |
|
$this->validator->setAttributeNames($fieldRules['attributes']); |
1218
|
|
|
|
1219
|
9 |
|
$this->eventDispatcher->fire(new BeforeFormValidation($this, $this->validator)); |
1220
|
|
|
|
1221
|
9 |
|
return $this->validator; |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
|
|
/** |
1225
|
|
|
* Get validation rules for the form. |
1226
|
|
|
* |
1227
|
|
|
* @param array $overrideRules |
1228
|
|
|
* @return array |
1229
|
|
|
*/ |
1230
|
1 |
|
public function getRules($overrideRules = []) |
1231
|
|
|
{ |
1232
|
1 |
|
$fieldRules = $this->formHelper->mergeFieldsRules($this->fields); |
1233
|
|
|
|
1234
|
1 |
|
return array_merge($fieldRules['rules'], $overrideRules); |
1235
|
|
|
} |
1236
|
|
|
|
1237
|
|
|
/** |
1238
|
|
|
* Redirects to a destination when form is invalid. |
1239
|
|
|
* |
1240
|
|
|
* @param string|null $destination The target url. |
1241
|
|
|
* @return HttpResponseException |
1242
|
|
|
*/ |
1243
|
3 |
|
public function redirectIfNotValid($destination = null) |
1244
|
|
|
{ |
1245
|
3 |
|
if (! $this->isValid()) { |
1246
|
3 |
|
$response = redirect($destination); |
1247
|
|
|
|
1248
|
3 |
|
if (is_null($destination)) { |
1249
|
2 |
|
$response = $response->back(); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
3 |
|
$response = $response->withErrors($this->getErrors(), $this->getErrorBag())->withInput(); |
1253
|
|
|
|
1254
|
3 |
|
throw new HttpResponseException($response); |
1255
|
|
|
} |
1256
|
|
|
} |
1257
|
|
|
|
1258
|
|
|
/** |
1259
|
|
|
* Get all form field attributes, including child forms, in a flat array. |
1260
|
|
|
* |
1261
|
|
|
* @return array |
1262
|
|
|
*/ |
1263
|
3 |
|
public function getAllAttributes() |
1264
|
|
|
{ |
1265
|
3 |
|
return $this->formHelper->mergeAttributes($this->fields); |
1266
|
|
|
} |
1267
|
|
|
|
1268
|
|
|
/** |
1269
|
|
|
* Check if the form is valid. |
1270
|
|
|
* |
1271
|
|
|
* @return bool |
1272
|
|
|
*/ |
1273
|
9 |
|
public function isValid() |
1274
|
|
|
{ |
1275
|
9 |
|
if (!$this->validator) { |
1276
|
8 |
|
$this->validate(); |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
9 |
|
$isValid = !$this->validator->fails(); |
1280
|
|
|
|
1281
|
9 |
|
$this->formHelper->alterValid($this, $this, $isValid); |
1282
|
|
|
|
1283
|
9 |
|
$this->eventDispatcher->fire(new AfterFormValidation($this, $this->validator, $isValid)); |
1284
|
|
|
|
1285
|
9 |
|
return $isValid; |
1286
|
|
|
} |
1287
|
|
|
|
1288
|
|
|
/** |
1289
|
|
|
* Optionally change the validation result, and/or add error messages. |
1290
|
|
|
* |
1291
|
|
|
* @param Form $mainForm |
1292
|
|
|
* @param bool $isValid |
1293
|
|
|
* @return void|array |
1294
|
|
|
*/ |
1295
|
9 |
|
public function alterValid(Form $mainForm, &$isValid) |
|
|
|
|
1296
|
|
|
{ |
1297
|
|
|
// return ['name' => ['Some other error about the Name field.']]; |
1298
|
9 |
|
} |
1299
|
|
|
|
1300
|
|
|
/** |
1301
|
|
|
* Get validation errors. |
1302
|
|
|
* |
1303
|
|
|
* @return array |
1304
|
|
|
*/ |
1305
|
8 |
|
public function getErrors() |
1306
|
|
|
{ |
1307
|
8 |
|
if (!$this->validator || !$this->validator instanceof Validator) { |
1308
|
1 |
|
throw new \InvalidArgumentException( |
1309
|
1 |
|
sprintf( |
1310
|
1 |
|
'Form %s was not validated. To validate it, call "isValid" method before retrieving the errors', |
1311
|
1 |
|
get_class($this) |
1312
|
|
|
) |
1313
|
|
|
); |
1314
|
|
|
} |
1315
|
|
|
|
1316
|
7 |
|
return $this->validator->getMessageBag()->getMessages(); |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
|
|
/** |
1320
|
|
|
* Get all Request values from all fields, and nothing else. |
1321
|
|
|
* |
1322
|
|
|
* @param bool $with_nulls |
1323
|
|
|
* @return array |
1324
|
|
|
*/ |
1325
|
3 |
|
public function getFieldValues($with_nulls = true) |
1326
|
|
|
{ |
1327
|
3 |
|
$request_values = $this->getRequest()->all(); |
1328
|
|
|
|
1329
|
3 |
|
$values = []; |
1330
|
3 |
|
foreach ($this->getAllAttributes() as $attribute) { |
1331
|
3 |
|
$value = Arr::get($request_values, $attribute); |
1332
|
3 |
|
if ($with_nulls || $value !== null) { |
1333
|
3 |
|
Arr::set($values, $attribute, $value); |
1334
|
|
|
} |
1335
|
|
|
} |
1336
|
|
|
|
1337
|
|
|
// If this form is a child form, cherry pick a part |
1338
|
3 |
|
if ($prefix = $this->getName()) { |
1339
|
1 |
|
$prefix = $this->formHelper->transformToDotSyntax($prefix); |
1340
|
1 |
|
$values = Arr::get($values, $prefix); |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
// Allow form-specific value alters |
1344
|
3 |
|
$this->formHelper->alterFieldValues($this, $values); |
1345
|
|
|
|
1346
|
3 |
|
return $values; |
1347
|
|
|
} |
1348
|
|
|
|
1349
|
|
|
/** |
1350
|
|
|
* Optionally mess with this form's $values before it's returned from getFieldValues(). |
1351
|
|
|
* |
1352
|
|
|
* @param array $values |
1353
|
|
|
* @return void |
1354
|
|
|
*/ |
1355
|
3 |
|
public function alterFieldValues(array &$values) |
|
|
|
|
1356
|
|
|
{ |
1357
|
3 |
|
} |
1358
|
|
|
|
1359
|
|
|
/** |
1360
|
|
|
* Throw an exception indicating a field does not exist on the class. |
1361
|
|
|
* |
1362
|
|
|
* @param string $name |
1363
|
|
|
* @throws \InvalidArgumentException |
1364
|
|
|
* @return void |
1365
|
|
|
*/ |
1366
|
2 |
|
protected function fieldDoesNotExist($name) |
1367
|
|
|
{ |
1368
|
2 |
|
throw new \InvalidArgumentException('Field ['.$name.'] does not exist in '.get_class($this)); |
1369
|
|
|
} |
1370
|
|
|
|
1371
|
|
|
/** |
1372
|
|
|
* Method filterFields used as *Main* method for starting |
1373
|
|
|
* filtering and request field mutating process. |
1374
|
|
|
* |
1375
|
|
|
* @return \Kris\LaravelFormBuilder\Form |
1376
|
|
|
*/ |
1377
|
129 |
|
public function filterFields() |
1378
|
|
|
{ |
1379
|
|
|
// If filtering is unlocked/allowed we can start with filtering process. |
1380
|
129 |
|
if (!$this->isFilteringLocked()) { |
1381
|
|
|
// Init required vars. |
1382
|
129 |
|
$filters = $this->getFilters(); |
1383
|
129 |
|
$request = $this->getRequest(); |
1384
|
|
|
|
1385
|
129 |
|
if (!empty($filters)) { |
1386
|
16 |
|
foreach ($filters as $field => $fieldFilters) { |
1387
|
|
|
// If field exist in request object, try to mutate/filter |
1388
|
|
|
// it to filtered value if there is one. |
1389
|
16 |
|
if (array_key_exists($field, $request->all())) { |
1390
|
|
|
// Assign current Raw/Unmutated value from request. |
1391
|
1 |
|
$this->fields[$field]->setRawValue($request[$field]); |
1392
|
1 |
|
foreach ($fieldFilters as $filter) { |
1393
|
1 |
|
$filterObj = FilterResolver::instance($filter); |
1394
|
16 |
|
$request[$field] = $filterObj->filter($request[$field]); |
1395
|
|
|
} |
1396
|
|
|
} |
1397
|
|
|
} |
1398
|
|
|
} |
1399
|
|
|
} |
1400
|
|
|
|
1401
|
129 |
|
return $this; |
1402
|
|
|
} |
1403
|
|
|
|
1404
|
|
|
/** |
1405
|
|
|
* Method getFilters used to return array of all binded filters to form fields. |
1406
|
|
|
* |
1407
|
|
|
* @return array |
1408
|
|
|
*/ |
1409
|
129 |
|
public function getFilters() |
1410
|
|
|
{ |
1411
|
129 |
|
$filters = []; |
1412
|
129 |
|
foreach ($this->getFields() as $field) { |
1413
|
17 |
|
$filters[$field->getName()] = $field->getFilters(); |
1414
|
|
|
} |
1415
|
|
|
|
1416
|
129 |
|
return $filters; |
1417
|
|
|
} |
1418
|
|
|
|
1419
|
|
|
/** |
1420
|
|
|
* If lockFiltering is set to true then we will not |
1421
|
|
|
* filter fields and mutate request data binded to fields. |
1422
|
|
|
* |
1423
|
|
|
* @return \Kris\LaravelFormBuilder\Form |
1424
|
|
|
*/ |
1425
|
1 |
|
public function lockFiltering() |
1426
|
|
|
{ |
1427
|
1 |
|
$this->lockFiltering = true; |
1428
|
1 |
|
return $this; |
1429
|
|
|
} |
1430
|
|
|
|
1431
|
|
|
/** |
1432
|
|
|
* Unlock fields filtering/mutating. |
1433
|
|
|
* |
1434
|
|
|
* @return \Kris\LaravelFormBuilder\Form |
1435
|
|
|
*/ |
1436
|
|
|
public function unlockFiltering() |
1437
|
|
|
{ |
1438
|
|
|
$this->lockFiltering = false; |
1439
|
|
|
return $this; |
1440
|
|
|
} |
1441
|
|
|
|
1442
|
|
|
/** |
1443
|
|
|
* Method isFilteringLocked used to check |
1444
|
|
|
* if current filteringLocked property status is set to true. |
1445
|
|
|
* |
1446
|
|
|
* @return bool |
1447
|
|
|
*/ |
1448
|
129 |
|
public function isFilteringLocked() |
1449
|
|
|
{ |
1450
|
129 |
|
return !$this->lockFiltering ? false : true; |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
|
|
/** |
1454
|
|
|
* Method getRawValues returns Unfiltered/Unmutated fields -> values. |
1455
|
|
|
* |
1456
|
|
|
* @return array |
1457
|
|
|
*/ |
1458
|
|
|
public function getRawValues() |
1459
|
|
|
{ |
1460
|
|
|
$rawValues = []; |
1461
|
|
|
foreach ($this->getFields() as $field) { |
1462
|
|
|
$rawValues[$field->getName()] = $field->getRawValue(); |
1463
|
|
|
} |
1464
|
|
|
|
1465
|
|
|
return $rawValues; |
1466
|
|
|
} |
1467
|
|
|
} |
1468
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: