Complex classes like Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Form |
||
18 | { |
||
19 | /** |
||
20 | * All fields that are added. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $fields = []; |
||
25 | |||
26 | /** |
||
27 | * Model to use. |
||
28 | * |
||
29 | * @var mixed |
||
30 | */ |
||
31 | protected $model = []; |
||
32 | |||
33 | /** |
||
34 | * @var EventDispatcher |
||
35 | */ |
||
36 | protected $eventDispatcher; |
||
37 | |||
38 | /** |
||
39 | * @var FormHelper |
||
40 | */ |
||
41 | protected $formHelper; |
||
42 | |||
43 | /** |
||
44 | * Form options. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $formOptions = [ |
||
49 | 'method' => 'GET', |
||
50 | 'url' => null |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Additional data which can be used to build fields. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $data = []; |
||
59 | |||
60 | /** |
||
61 | * Wether errors for each field should be shown when calling form($form) or form_rest($form). |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $showFieldErrors = true; |
||
66 | |||
67 | /** |
||
68 | * Enable html5 validation. |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $clientValidationEnabled = true; |
||
73 | |||
74 | /** |
||
75 | * Name of the parent form if any. |
||
76 | * |
||
77 | * @var string|null |
||
78 | */ |
||
79 | protected $name = null; |
||
80 | |||
81 | /** |
||
82 | * @var FormBuilder |
||
83 | */ |
||
84 | protected $formBuilder; |
||
85 | |||
86 | /** |
||
87 | * @var ValidatorFactory |
||
88 | */ |
||
89 | protected $validatorFactory; |
||
90 | |||
91 | /** |
||
92 | * @var Validator |
||
93 | */ |
||
94 | protected $validator = null; |
||
95 | |||
96 | /** |
||
97 | * @var Request |
||
98 | */ |
||
99 | protected $request; |
||
100 | |||
101 | /** |
||
102 | * List of fields to not render. |
||
103 | * |
||
104 | * @var array |
||
105 | **/ |
||
106 | protected $exclude = []; |
||
107 | |||
108 | /** |
||
109 | * Wether the form is beign rebuild. |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $rebuilding = false; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $templatePrefix; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $languageName; |
||
124 | |||
125 | /** |
||
126 | * @var string |
||
127 | */ |
||
128 | protected $translationTemplate; |
||
129 | |||
130 | /** |
||
131 | * To filter and mutate request values or not. |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | protected $lockFiltering = false; |
||
136 | |||
137 | /** |
||
138 | * Build the form. |
||
139 | * |
||
140 | * @return mixed |
||
141 | */ |
||
142 | 3 | public function buildForm() |
|
143 | { |
||
144 | 3 | } |
|
145 | |||
146 | /** |
||
147 | * Rebuild the form from scratch. |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | 19 | public function rebuildForm() |
|
152 | { |
||
153 | 19 | $this->rebuilding = true; |
|
154 | // If form is plain, buildForm method is empty, so we need to take |
||
155 | // existing fields and add them again |
||
156 | 19 | if (get_class($this) === 'Kris\LaravelFormBuilder\Form') { |
|
157 | 18 | foreach ($this->fields as $name => $field) { |
|
158 | // Remove any temp variables added in previous instance |
||
159 | 7 | $options = array_except($field->getOptions(), 'tmp'); |
|
160 | 18 | $this->add($name, $field->getType(), $options); |
|
161 | } |
||
162 | } else { |
||
163 | 3 | $this->buildForm(); |
|
164 | } |
||
165 | 19 | $this->rebuilding = false; |
|
166 | |||
167 | 19 | return $this; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Create the FormField object. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @param string $type |
||
175 | * @param array $options |
||
176 | * @return FormField |
||
177 | */ |
||
178 | 65 | protected function makeField($name, $type = 'text', array $options = []) |
|
179 | { |
||
180 | 65 | $this->setupFieldOptions($name, $options); |
|
181 | |||
182 | 65 | $fieldName = $this->getFieldName($name); |
|
183 | |||
184 | 65 | $fieldType = $this->getFieldType($type); |
|
185 | |||
186 | 64 | $field = new $fieldType($fieldName, $type, $this, $options); |
|
187 | |||
188 | 61 | $this->eventDispatcher->fire(new AfterFieldCreation($this, $field)); |
|
189 | |||
190 | 61 | return $field; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Create a new field and add it to the form. |
||
195 | * |
||
196 | * @param string $name |
||
197 | * @param string $type |
||
198 | * @param array $options |
||
199 | * @param bool $modify |
||
200 | * @return $this |
||
201 | */ |
||
202 | 67 | public function add($name, $type = 'text', array $options = [], $modify = false) |
|
203 | { |
||
204 | 67 | $this->formHelper->checkFieldName($name, get_class($this)); |
|
205 | |||
206 | 65 | if ($this->rebuilding && !$this->has($name)) { |
|
207 | return $this; |
||
208 | } |
||
209 | |||
210 | 65 | $this->addField($this->makeField($name, $type, $options), $modify); |
|
211 | |||
212 | 61 | return $this; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Add a FormField to the form's fields. |
||
217 | * |
||
218 | * @param FormField $field |
||
219 | * @return $this |
||
220 | */ |
||
221 | 61 | protected function addField(FormField $field, $modify = false) |
|
222 | { |
||
223 | 61 | if (!$modify && !$this->rebuilding) { |
|
224 | 61 | $this->preventDuplicate($field->getRealName()); |
|
225 | } |
||
226 | |||
227 | |||
228 | 61 | if ($field->getType() == 'file') { |
|
229 | 3 | $this->formOptions['files'] = true; |
|
230 | } |
||
231 | |||
232 | 61 | $this->fields[$field->getRealName()] = $field; |
|
233 | |||
234 | 61 | return $this; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Add field before another field. |
||
239 | * |
||
240 | * @param string $name Name of the field before which new field is added. |
||
241 | * @param string $fieldName Field name which will be added. |
||
242 | * @param string $type |
||
243 | * @param array $options |
||
244 | * @param bool $modify |
||
245 | * @return $this |
||
246 | */ |
||
247 | 1 | public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
248 | { |
||
249 | 1 | $offset = array_search($name, array_keys($this->fields)); |
|
250 | |||
251 | 1 | $beforeFields = array_slice($this->fields, 0, $offset); |
|
252 | 1 | $afterFields = array_slice($this->fields, $offset); |
|
253 | |||
254 | 1 | $this->fields = $beforeFields; |
|
255 | |||
256 | 1 | $this->add($fieldName, $type, $options, $modify); |
|
257 | |||
258 | 1 | $this->fields += $afterFields; |
|
259 | |||
260 | 1 | return $this; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Add field before another field. |
||
265 | * |
||
266 | * @param string $name Name of the field after which new field is added. |
||
267 | * @param string $fieldName Field name which will be added. |
||
268 | * @param string $type |
||
269 | * @param array $options |
||
270 | * @param bool $modify |
||
271 | * @return $this |
||
272 | */ |
||
273 | 1 | public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
274 | { |
||
275 | 1 | $offset = array_search($name, array_keys($this->fields)); |
|
276 | |||
277 | 1 | $beforeFields = array_slice($this->fields, 0, $offset + 1); |
|
278 | 1 | $afterFields = array_slice($this->fields, $offset + 1); |
|
279 | |||
280 | 1 | $this->fields = $beforeFields; |
|
281 | |||
282 | 1 | $this->add($fieldName, $type, $options, $modify); |
|
283 | |||
284 | 1 | $this->fields += $afterFields; |
|
285 | |||
286 | 1 | return $this; |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Take another form and add it's fields directly to this form. |
||
291 | * |
||
292 | * @param mixed $class Form to merge. |
||
293 | * @param array $options |
||
294 | * @param boolean $modify |
||
295 | * @return $this |
||
296 | */ |
||
297 | 1 | public function compose($class, array $options = [], $modify = false) |
|
298 | { |
||
299 | 1 | $options['class'] = $class; |
|
300 | |||
301 | // If we pass a ready made form just extract the fields. |
||
302 | 1 | if ($class instanceof Form) { |
|
303 | 1 | $fields = $class->getFields(); |
|
304 | } elseif ($class instanceof Fields\ChildFormType) { |
||
305 | $fields = $class->getForm()->getFields(); |
||
306 | } elseif (is_string($class)) { |
||
307 | // If its a string of a class make it the usual way. |
||
308 | $options['model'] = $this->model; |
||
309 | $options['name'] = $this->name; |
||
310 | |||
311 | $form = $this->formBuilder->create($class, $options); |
||
312 | $fields = $form->getFields(); |
||
313 | } else { |
||
314 | throw new \InvalidArgumentException( |
||
315 | "[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType" |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | 1 | foreach ($fields as $field) { |
|
320 | 1 | $this->addField($field, $modify); |
|
321 | } |
||
322 | |||
323 | 1 | return $this; |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Remove field with specified name from the form. |
||
328 | * |
||
329 | * @param $name |
||
330 | * @return $this |
||
331 | */ |
||
332 | 2 | public function remove($name) |
|
333 | { |
||
334 | 2 | if ($this->has($name)) { |
|
335 | 2 | unset($this->fields[$name]); |
|
336 | } |
||
337 | |||
338 | 2 | return $this; |
|
339 | } |
||
340 | |||
341 | /** |
||
342 | * Modify existing field. If it doesn't exist, it is added to form. |
||
343 | * |
||
344 | * @param string $name |
||
345 | * @param string $type |
||
346 | * @param array $options |
||
347 | * @param bool $overwriteOptions |
||
348 | * @return Form |
||
349 | */ |
||
350 | 1 | public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
|
351 | { |
||
352 | // If we don't want to overwrite options, we merge them with old options. |
||
353 | 1 | if ($overwriteOptions === false && $this->has($name)) { |
|
354 | 1 | $options = $this->formHelper->mergeOptions( |
|
355 | 1 | $this->getField($name)->getOptions(), |
|
356 | 1 | $options |
|
357 | ); |
||
358 | } |
||
359 | |||
360 | 1 | return $this->add($name, $type, $options, true); |
|
361 | } |
||
362 | |||
363 | /** |
||
364 | * Render full form. |
||
365 | * |
||
366 | * @param array $options |
||
367 | * @param bool $showStart |
||
368 | * @param bool $showFields |
||
369 | * @param bool $showEnd |
||
370 | * @return string |
||
371 | */ |
||
372 | 7 | public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
|
373 | { |
||
374 | 7 | return $this->render($options, $this->fields, $showStart, $showFields, $showEnd); |
|
|
|||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Render rest of the form. |
||
379 | * |
||
380 | * @param bool $showFormEnd |
||
381 | * @param bool $showFields |
||
382 | * @return string |
||
383 | */ |
||
384 | 1 | public function renderRest($showFormEnd = true, $showFields = true) |
|
385 | { |
||
386 | 1 | $fields = $this->getUnrenderedFields(); |
|
387 | |||
388 | 1 | return $this->render([], $fields, false, $showFields, $showFormEnd); |
|
389 | } |
||
390 | |||
391 | /** |
||
392 | * Renders the rest of the form up until the specified field name. |
||
393 | * |
||
394 | * @param string $field_name |
||
395 | * @param bool $showFormEnd |
||
396 | * @param bool $showFields |
||
397 | * @return string |
||
398 | */ |
||
399 | 2 | public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
|
400 | { |
||
401 | 2 | if (!$this->has($field_name)) { |
|
402 | 1 | $this->fieldDoesNotExist($field_name); |
|
403 | } |
||
404 | |||
405 | 1 | $fields = $this->getUnrenderedFields(); |
|
406 | |||
407 | 1 | $i = 1; |
|
408 | 1 | foreach ($fields as $key => $value) { |
|
409 | 1 | if ($value->getRealName() == $field_name) { |
|
410 | 1 | break; |
|
411 | } |
||
412 | 1 | $i++; |
|
413 | } |
||
414 | |||
415 | 1 | $fields = array_slice($fields, 0, $i, true); |
|
416 | |||
417 | 1 | return $this->render([], $fields, false, $showFields, $showFormEnd); |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * Get single field instance from form object. |
||
422 | * |
||
423 | * @param string $name |
||
424 | * @return FormField |
||
425 | */ |
||
426 | 34 | public function getField($name) |
|
427 | { |
||
428 | 34 | if ($this->has($name)) { |
|
429 | 33 | return $this->fields[$name]; |
|
430 | } |
||
431 | |||
432 | 1 | $this->fieldDoesNotExist($name); |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Check if form has field. |
||
437 | * |
||
438 | * @param string $name |
||
439 | * @return bool |
||
440 | */ |
||
441 | 61 | public function has($name) |
|
442 | { |
||
443 | 61 | return array_key_exists($name, $this->fields); |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * Get all form options. |
||
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | 2 | public function getFormOptions() |
|
452 | { |
||
453 | 2 | return $this->formOptions; |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * Get single form option. |
||
458 | * |
||
459 | * @param string $option |
||
460 | * @param mixed|null $default |
||
461 | * @return mixed |
||
462 | */ |
||
463 | 129 | public function getFormOption($option, $default = null) |
|
464 | { |
||
465 | 129 | return array_get($this->formOptions, $option, $default); |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * Set single form option on form. |
||
470 | * |
||
471 | * @param string $option |
||
472 | * @param mixed $value |
||
473 | * |
||
474 | * @return $this |
||
475 | */ |
||
476 | 2 | public function setFormOption($option, $value) |
|
477 | { |
||
478 | 2 | $this->formOptions[$option] = $value; |
|
479 | |||
480 | 2 | return $this; |
|
481 | } |
||
482 | |||
483 | /** |
||
484 | * Set form options. |
||
485 | * |
||
486 | * @param array $formOptions |
||
487 | * @return $this |
||
488 | */ |
||
489 | 129 | public function setFormOptions(array $formOptions) |
|
490 | { |
||
491 | 129 | $this->formOptions = $this->formHelper->mergeOptions($this->formOptions, $formOptions); |
|
492 | 129 | $this->checkIfNamedForm(); |
|
493 | 129 | $this->pullFromOptions('data', 'addData'); |
|
494 | 129 | $this->pullFromOptions('model', 'setupModel'); |
|
495 | 129 | $this->pullFromOptions('errors_enabled', 'setErrorsEnabled'); |
|
496 | 129 | $this->pullFromOptions('client_validation', 'setClientValidationEnabled'); |
|
497 | 129 | $this->pullFromOptions('template_prefix', 'setTemplatePrefix'); |
|
498 | 129 | $this->pullFromOptions('language_name', 'setLanguageName'); |
|
499 | 129 | $this->pullFromOptions('translation_template', 'setTranslationTemplate'); |
|
500 | |||
501 | 129 | return $this; |
|
502 | } |
||
503 | |||
504 | /** |
||
505 | * Get an option from provided options and call method with that value. |
||
506 | * |
||
507 | * @param string $name |
||
508 | * @param string $method |
||
509 | */ |
||
510 | 129 | protected function pullFromOptions($name, $method) |
|
511 | { |
||
512 | 129 | if (array_get($this->formOptions, $name) !== null) { |
|
513 | 20 | $this->{$method}(array_pull($this->formOptions, $name)); |
|
514 | } |
||
515 | 129 | } |
|
516 | |||
517 | /** |
||
518 | * Get form http method. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | 3 | public function getMethod() |
|
523 | { |
||
524 | 3 | return $this->formOptions['method']; |
|
525 | } |
||
526 | |||
527 | /** |
||
528 | * Set form http method. |
||
529 | * |
||
530 | * @param string $method |
||
531 | * @return $this |
||
532 | */ |
||
533 | 1 | public function setMethod($method) |
|
534 | { |
||
535 | 1 | $this->formOptions['method'] = $method; |
|
536 | |||
537 | 1 | return $this; |
|
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get form action url. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | 3 | public function getUrl() |
|
546 | { |
||
547 | 3 | return $this->formOptions['url']; |
|
548 | } |
||
549 | |||
550 | /** |
||
551 | * Set form action url. |
||
552 | * |
||
553 | * @param string $url |
||
554 | * @return $this |
||
555 | */ |
||
556 | 1 | public function setUrl($url) |
|
557 | { |
||
558 | 1 | $this->formOptions['url'] = $url; |
|
559 | |||
560 | 1 | return $this; |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * Returns the name of the form. |
||
565 | * |
||
566 | * @return string|null |
||
567 | */ |
||
568 | 66 | public function getName() |
|
572 | |||
573 | /** |
||
574 | * Set the name of the form. |
||
575 | * |
||
576 | * @param string $name |
||
577 | * @param bool $rebuild |
||
578 | * @return $this |
||
579 | */ |
||
580 | 12 | public function setName($name, $rebuild = true) |
|
581 | { |
||
582 | 12 | $this->name = $name; |
|
583 | |||
584 | 12 | if ($rebuild) { |
|
585 | 12 | $this->rebuildForm(); |
|
586 | } |
||
587 | |||
588 | 12 | return $this; |
|
589 | } |
||
590 | |||
591 | /** |
||
592 | * Get model that is bind to form object. |
||
593 | * |
||
594 | * @return mixed |
||
595 | */ |
||
596 | 96 | public function getModel() |
|
597 | { |
||
600 | |||
601 | /** |
||
602 | * Set model to form object. |
||
603 | * |
||
604 | * @param mixed $model |
||
605 | * @return $this |
||
606 | * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
||
607 | */ |
||
608 | 17 | public function setModel($model) |
|
616 | |||
617 | /** |
||
618 | * Setup model for form, add namespace if needed for child forms. |
||
619 | * |
||
620 | * @return $this |
||
621 | */ |
||
622 | 12 | protected function setupModel($model) |
|
628 | |||
629 | /** |
||
630 | * Get all fields. |
||
631 | * |
||
632 | * @return FormField[] |
||
633 | */ |
||
634 | 129 | public function getFields() |
|
638 | |||
639 | /** |
||
640 | * Get field dynamically. |
||
641 | * |
||
642 | * @param string $name |
||
643 | * @return FormField |
||
644 | */ |
||
645 | 20 | public function __get($name) |
|
651 | |||
652 | /** |
||
653 | * Check if field exists when fetched using magic methods. |
||
654 | * |
||
655 | * @param string $name |
||
656 | * @return bool |
||
657 | */ |
||
658 | public function __isset($name) |
||
662 | |||
663 | /** |
||
664 | * Set the Event Dispatcher to fire Laravel events. |
||
665 | * |
||
666 | * @param EventDispatcher $eventDispatcher |
||
667 | * @return $this |
||
668 | */ |
||
669 | 129 | public function setEventDispatcher(EventDispatcher $eventDispatcher) |
|
675 | |||
676 | /** |
||
677 | * Set the form helper only on first instantiation. |
||
678 | * |
||
679 | * @param FormHelper $formHelper |
||
680 | * @return $this |
||
681 | */ |
||
682 | 129 | public function setFormHelper(FormHelper $formHelper) |
|
688 | |||
689 | /** |
||
690 | * Get form helper. |
||
691 | * |
||
692 | * @return FormHelper |
||
693 | */ |
||
694 | 101 | public function getFormHelper() |
|
698 | |||
699 | /** |
||
700 | * Add custom field. |
||
701 | * |
||
702 | * @param $name |
||
703 | * @param $class |
||
704 | */ |
||
705 | 2 | public function addCustomField($name, $class) |
|
713 | |||
714 | /** |
||
715 | * Returns wether form errors should be shown under every field. |
||
716 | * |
||
717 | * @return bool |
||
718 | */ |
||
719 | 101 | public function haveErrorsEnabled() |
|
723 | |||
724 | /** |
||
725 | * Enable or disable showing errors under fields |
||
726 | * |
||
727 | * @param bool $enabled |
||
728 | * @return $this |
||
729 | */ |
||
730 | 1 | public function setErrorsEnabled($enabled) |
|
736 | |||
737 | /** |
||
738 | * Is client validation enabled? |
||
739 | * |
||
740 | * @return bool |
||
741 | */ |
||
742 | 101 | public function clientValidationEnabled() |
|
746 | |||
747 | /** |
||
748 | * Enable/disable client validation. |
||
749 | * |
||
750 | * @param bool $enable |
||
751 | * @return $this |
||
752 | */ |
||
753 | 2 | public function setClientValidationEnabled($enable) |
|
759 | |||
760 | /** |
||
761 | * Add any aditional data that field needs (ex. array of choices). |
||
762 | * |
||
763 | * @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 |
||
764 | * will be switched to protected in 1.7. |
||
765 | * @param string $name |
||
766 | * @param mixed $data |
||
767 | */ |
||
768 | 1 | public function setData($name, $data) |
|
772 | |||
773 | /** |
||
774 | * Get single additional data. |
||
775 | * |
||
776 | * @param string $name |
||
777 | * @param null $default |
||
778 | * @return mixed |
||
779 | */ |
||
780 | 20 | public function getData($name = null, $default = null) |
|
788 | |||
789 | /** |
||
790 | * Add multiple peices of data at once. |
||
791 | * |
||
792 | * @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 |
||
793 | * will be switched to protected in 1.7. |
||
794 | * @param $data |
||
795 | * @return $this |
||
796 | **/ |
||
797 | 129 | public function addData(array $data) |
|
805 | |||
806 | /** |
||
807 | * Get current request. |
||
808 | * |
||
809 | * @return \Illuminate\Http\Request |
||
810 | */ |
||
811 | 129 | public function getRequest() |
|
815 | |||
816 | /** |
||
817 | * Set request on form. |
||
818 | * |
||
819 | * @param Request $request |
||
820 | * @return $this |
||
821 | */ |
||
822 | 129 | public function setRequest(Request $request) |
|
828 | |||
829 | /** |
||
830 | * Get template prefix that is prepended to all template paths. |
||
831 | * |
||
832 | * @return string |
||
833 | */ |
||
834 | 39 | public function getTemplatePrefix() |
|
842 | |||
843 | /** |
||
844 | * Set a template prefix for the form and its fields. |
||
845 | * |
||
846 | * @param string $prefix |
||
847 | * @return $this |
||
848 | */ |
||
849 | 4 | public function setTemplatePrefix($prefix) |
|
855 | |||
856 | /** |
||
857 | * Get the language name. |
||
858 | * |
||
859 | * @return string |
||
860 | */ |
||
861 | 98 | public function getLanguageName() |
|
865 | |||
866 | /** |
||
867 | * Set a language name, used as prefix for translated strings. |
||
868 | * |
||
869 | * @param string $prefix |
||
870 | * @return $this |
||
871 | */ |
||
872 | 13 | public function setLanguageName($prefix) |
|
878 | |||
879 | /** |
||
880 | * Get the translation template. |
||
881 | * |
||
882 | * @return string |
||
883 | */ |
||
884 | 100 | public function getTranslationTemplate() |
|
888 | |||
889 | /** |
||
890 | * Set a translation template, used to determine labels for fields. |
||
891 | * |
||
892 | * @param string $template |
||
893 | * @return $this |
||
894 | */ |
||
895 | 11 | public function setTranslationTemplate($template) |
|
901 | |||
902 | /** |
||
903 | * Render the form. |
||
904 | * |
||
905 | * @param array $options |
||
906 | * @param string $fields |
||
907 | * @param bool $showStart |
||
908 | * @param bool $showFields |
||
909 | * @param bool $showEnd |
||
910 | * @return string |
||
911 | */ |
||
912 | 9 | protected function render($options, $fields, $showStart, $showFields, $showEnd) |
|
928 | |||
929 | /** |
||
930 | * Get template from options if provided, otherwise fallback to config. |
||
931 | * |
||
932 | * @return mixed |
||
933 | */ |
||
934 | 9 | protected function getTemplate() |
|
938 | |||
939 | /** |
||
940 | * Get all fields that are not rendered. |
||
941 | * |
||
942 | * @return array |
||
943 | */ |
||
944 | 2 | protected function getUnrenderedFields() |
|
957 | |||
958 | /** |
||
959 | * Prevent adding fields with same name. |
||
960 | * |
||
961 | * @param string $name |
||
962 | * @throws \InvalidArgumentException |
||
963 | * @return void |
||
964 | */ |
||
965 | 61 | protected function preventDuplicate($name) |
|
971 | |||
972 | /** |
||
973 | * Returns and checks the type of the field. |
||
974 | * |
||
975 | * @param string $type |
||
976 | * @return string |
||
977 | */ |
||
978 | 65 | protected function getFieldType($type) |
|
984 | |||
985 | /** |
||
986 | * Check if form is named form. |
||
987 | * |
||
988 | * @return void |
||
989 | */ |
||
990 | 129 | protected function checkIfNamedForm() |
|
996 | |||
997 | /** |
||
998 | * Set up options on single field depending on form options. |
||
999 | * |
||
1000 | * @param string $name |
||
1001 | * @param $options |
||
1002 | */ |
||
1003 | 65 | protected function setupFieldOptions($name, &$options) |
|
1007 | |||
1008 | /** |
||
1009 | * Set namespace to model if form is named so the data is bound properly. |
||
1010 | * Returns true if model is changed, otherwise false. |
||
1011 | * |
||
1012 | * @return bool |
||
1013 | */ |
||
1014 | 9 | protected function setupNamedModel() |
|
1033 | |||
1034 | /** |
||
1035 | * Set form builder instance on helper so we can use it later. |
||
1036 | * |
||
1037 | * @param FormBuilder $formBuilder |
||
1038 | * @return $this |
||
1039 | */ |
||
1040 | 129 | public function setFormBuilder(FormBuilder $formBuilder) |
|
1046 | |||
1047 | /** |
||
1048 | * Returns the instance of the FormBuilder. |
||
1049 | * |
||
1050 | * @return FormBuilder |
||
1051 | */ |
||
1052 | 12 | public function getFormBuilder() |
|
1056 | |||
1057 | /** |
||
1058 | * Set the Validator instance on this so we can use it later. |
||
1059 | * |
||
1060 | * @param ValidatorFactory $validator |
||
1061 | * @return $this |
||
1062 | */ |
||
1063 | 129 | public function setValidator(ValidatorFactory $validator) |
|
1069 | |||
1070 | /** |
||
1071 | * Returns the validator instance. |
||
1072 | * |
||
1073 | * @return Validator |
||
1074 | */ |
||
1075 | 1 | public function getValidator() |
|
1079 | |||
1080 | /** |
||
1081 | * Exclude some fields from rendering. |
||
1082 | * |
||
1083 | * @return $this |
||
1084 | */ |
||
1085 | public function exclude(array $fields) |
||
1091 | |||
1092 | /** |
||
1093 | * If form is named form, modify names to be contained in single key (parent[child_field_name]). |
||
1094 | * |
||
1095 | * @param string $name |
||
1096 | * @return string |
||
1097 | */ |
||
1098 | 65 | protected function getFieldName($name) |
|
1115 | |||
1116 | /** |
||
1117 | * Disable all fields in a form. |
||
1118 | */ |
||
1119 | 1 | public function disableFields() |
|
1125 | |||
1126 | /** |
||
1127 | * Enable all fields in a form. |
||
1128 | */ |
||
1129 | 1 | public function enableFields() |
|
1135 | |||
1136 | /** |
||
1137 | * Validate the form. |
||
1138 | * |
||
1139 | * @param array $validationRules |
||
1140 | * @param array $messages |
||
1141 | * @return Validator |
||
1142 | */ |
||
1143 | 9 | public function validate($validationRules = [], $messages = []) |
|
1156 | |||
1157 | /** |
||
1158 | * Get validation rules for the form. |
||
1159 | * |
||
1160 | * @param array $overrideRules |
||
1161 | * @return array |
||
1162 | */ |
||
1163 | 1 | public function getRules($overrideRules = []) |
|
1169 | |||
1170 | /** |
||
1171 | * Redirects to a destination when form is invalid. |
||
1172 | * |
||
1173 | * @param string|null $destination The target url. |
||
1174 | * @return HttpResponseException |
||
1175 | */ |
||
1176 | 3 | public function redirectIfNotValid($destination = null) |
|
1190 | |||
1191 | /** |
||
1192 | * Get all form field attributes, including child forms, in a flat array. |
||
1193 | * |
||
1194 | * @return array |
||
1195 | */ |
||
1196 | 3 | public function getAllAttributes() |
|
1200 | |||
1201 | /** |
||
1202 | * Check if the form is valid. |
||
1203 | * |
||
1204 | * @return bool |
||
1205 | */ |
||
1206 | 9 | public function isValid() |
|
1220 | |||
1221 | /** |
||
1222 | * Optionally change the validation result, and/or add error messages. |
||
1223 | * |
||
1224 | * @param Form $mainForm |
||
1225 | * @param bool $isValid |
||
1226 | * @return void|array |
||
1227 | */ |
||
1228 | 9 | public function alterValid(Form $mainForm, &$isValid) |
|
1232 | |||
1233 | /** |
||
1234 | * Get validation errors. |
||
1235 | * |
||
1236 | * @return array |
||
1237 | */ |
||
1238 | 8 | public function getErrors() |
|
1251 | |||
1252 | /** |
||
1253 | * Get all Request values from all fields, and nothing else. |
||
1254 | * |
||
1255 | * @param bool $with_nulls |
||
1256 | * @return array |
||
1257 | */ |
||
1258 | 3 | public function getFieldValues($with_nulls = true) |
|
1281 | |||
1282 | /** |
||
1283 | * Optionally mess with this form's $values before it's returned from getFieldValues(). |
||
1284 | * |
||
1285 | * @param array $values |
||
1286 | * @return void |
||
1287 | */ |
||
1288 | 3 | public function alterFieldValues(array &$values) |
|
1291 | |||
1292 | /** |
||
1293 | * Throw an exception indicating a field does not exist on the class. |
||
1294 | * |
||
1295 | * @param string $name |
||
1296 | * @throws \InvalidArgumentException |
||
1297 | * @return void |
||
1298 | */ |
||
1299 | 2 | protected function fieldDoesNotExist($name) |
|
1303 | |||
1304 | /** |
||
1305 | * Method filterFields used as *Main* method for starting |
||
1306 | * filtering and request field mutating process. |
||
1307 | * |
||
1308 | * @return \Kris\LaravelFormBuilder\Form |
||
1309 | */ |
||
1310 | 129 | public function filterFields() |
|
1336 | |||
1337 | /** |
||
1338 | * Method getFilters used to return array of all binded filters to form fields. |
||
1339 | * |
||
1340 | * @return array |
||
1341 | */ |
||
1342 | 129 | public function getFilters() |
|
1351 | |||
1352 | /** |
||
1353 | * If lockFiltering is set to true then we will not |
||
1354 | * filter fields and mutate request data binded to fields. |
||
1355 | * |
||
1356 | * @return \Kris\LaravelFormBuilder\Form |
||
1357 | */ |
||
1358 | 1 | public function lockFiltering() |
|
1363 | |||
1364 | /** |
||
1365 | * Unlock fields filtering/mutating. |
||
1366 | * |
||
1367 | * @return \Kris\LaravelFormBuilder\Form |
||
1368 | */ |
||
1369 | public function unlockFiltering() |
||
1374 | |||
1375 | /** |
||
1376 | * Method isFilteringLocked used to check |
||
1377 | * if current filteringLocked property status is set to true. |
||
1378 | * |
||
1379 | * @return bool |
||
1380 | */ |
||
1381 | 129 | public function isFilteringLocked() |
|
1385 | |||
1386 | /** |
||
1387 | * Method getRawValues returns Unfiltered/Unmutated fields -> values. |
||
1388 | * |
||
1389 | * @return array |
||
1390 | */ |
||
1391 | public function getRawValues() |
||
1400 | } |
||
1401 |
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: