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 | * Form specific configuration. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $formConfig = []; |
||
59 | |||
60 | /** |
||
61 | * Additional data which can be used to build fields. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $data = []; |
||
66 | |||
67 | /** |
||
68 | * Wether errors for each field should be shown when calling form($form) or form_rest($form). |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $showFieldErrors = true; |
||
73 | |||
74 | /** |
||
75 | * Enable html5 validation. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $clientValidationEnabled = true; |
||
80 | |||
81 | /** |
||
82 | * Name of the parent form if any. |
||
83 | * |
||
84 | * @var string|null |
||
85 | */ |
||
86 | protected $name = null; |
||
87 | |||
88 | /** |
||
89 | * @var FormBuilder |
||
90 | */ |
||
91 | protected $formBuilder; |
||
92 | |||
93 | /** |
||
94 | * @var ValidatorFactory |
||
95 | */ |
||
96 | protected $validatorFactory; |
||
97 | |||
98 | /** |
||
99 | * @var Validator |
||
100 | */ |
||
101 | protected $validator = null; |
||
102 | |||
103 | /** |
||
104 | * @var Request |
||
105 | */ |
||
106 | protected $request; |
||
107 | |||
108 | /** |
||
109 | * List of fields to not render. |
||
110 | * |
||
111 | * @var array |
||
112 | **/ |
||
113 | protected $exclude = []; |
||
114 | |||
115 | /** |
||
116 | * Wether the form is beign rebuild. |
||
117 | * |
||
118 | * @var bool |
||
119 | */ |
||
120 | protected $rebuilding = false; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $templatePrefix; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $languageName; |
||
131 | |||
132 | /** |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $translationTemplate; |
||
136 | |||
137 | /** |
||
138 | * To filter and mutate request values or not. |
||
139 | * |
||
140 | * @var bool |
||
141 | */ |
||
142 | protected $lockFiltering = false; |
||
143 | |||
144 | /** |
||
145 | * Define the error bag name for the form. |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $errorBag = 'default'; |
||
150 | |||
151 | /** |
||
152 | * Build the form. |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | 3 | public function buildForm() |
|
159 | |||
160 | /** |
||
161 | * Rebuild the form from scratch. |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | 19 | public function rebuildForm() |
|
166 | { |
||
167 | 19 | $this->rebuilding = true; |
|
168 | // If form is plain, buildForm method is empty, so we need to take |
||
169 | // existing fields and add them again |
||
170 | 19 | if (get_class($this) === 'Kris\LaravelFormBuilder\Form') { |
|
171 | 18 | foreach ($this->fields as $name => $field) { |
|
172 | // Remove any temp variables added in previous instance |
||
173 | 7 | $options = Arr::except($field->getOptions(), 'tmp'); |
|
174 | 7 | $this->add($name, $field->getType(), $options); |
|
175 | 18 | } |
|
176 | 18 | } else { |
|
177 | 3 | $this->buildForm(); |
|
178 | } |
||
179 | 19 | $this->rebuilding = false; |
|
180 | |||
181 | 19 | return $this; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Create the FormField object. |
||
186 | * |
||
187 | * @param string $name |
||
188 | * @param string $type |
||
189 | * @param array $options |
||
190 | * @return FormField |
||
191 | */ |
||
192 | 65 | protected function makeField($name, $type = 'text', array $options = []) |
|
206 | |||
207 | /** |
||
208 | * Create a new field and add it to the form. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * @param string $type |
||
212 | * @param array $options |
||
213 | * @param bool $modify |
||
214 | * @return $this |
||
215 | */ |
||
216 | 67 | public function add($name, $type = 'text', array $options = [], $modify = false) |
|
228 | |||
229 | /** |
||
230 | * Add a FormField to the form's fields. |
||
231 | * |
||
232 | * @param FormField $field |
||
233 | * @return $this |
||
234 | */ |
||
235 | 61 | protected function addField(FormField $field, $modify = false) |
|
236 | { |
||
237 | 61 | if (!$modify && !$this->rebuilding) { |
|
238 | 61 | $this->preventDuplicate($field->getRealName()); |
|
239 | 61 | } |
|
240 | |||
241 | |||
242 | 61 | if ($field->getType() == 'file') { |
|
243 | 3 | $this->formOptions['files'] = true; |
|
244 | 3 | } |
|
245 | |||
246 | 61 | $this->fields[$field->getRealName()] = $field; |
|
247 | |||
248 | 61 | return $this; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Add field before another field. |
||
253 | * |
||
254 | * @param string $name Name of the field before which new field is added. |
||
255 | * @param string $fieldName Field name which will be added. |
||
256 | * @param string $type |
||
257 | * @param array $options |
||
258 | * @param bool $modify |
||
259 | * @return $this |
||
260 | */ |
||
261 | 1 | public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
276 | |||
277 | /** |
||
278 | * Add field before another field. |
||
279 | * |
||
280 | * @param string $name Name of the field after which new field is added. |
||
281 | * @param string $fieldName Field name which will be added. |
||
282 | * @param string $type |
||
283 | * @param array $options |
||
284 | * @param bool $modify |
||
285 | * @return $this |
||
286 | */ |
||
287 | 1 | public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
302 | |||
303 | /** |
||
304 | * Take another form and add it's fields directly to this form. |
||
305 | * |
||
306 | * @param mixed $class Form to merge. |
||
307 | * @param array $options |
||
308 | * @param boolean $modify |
||
309 | * @return $this |
||
310 | */ |
||
311 | 1 | public function compose($class, array $options = [], $modify = false) |
|
312 | { |
||
313 | 1 | $options['class'] = $class; |
|
314 | |||
315 | // If we pass a ready made form just extract the fields. |
||
316 | 1 | if ($class instanceof Form) { |
|
317 | 1 | $fields = $class->getFields(); |
|
318 | 1 | } elseif ($class instanceof Fields\ChildFormType) { |
|
319 | $fields = $class->getForm()->getFields(); |
||
320 | } elseif (is_string($class)) { |
||
321 | // If its a string of a class make it the usual way. |
||
322 | $options['model'] = $this->model; |
||
323 | $options['name'] = $this->name; |
||
324 | |||
325 | $form = $this->formBuilder->create($class, $options); |
||
326 | $fields = $form->getFields(); |
||
327 | } else { |
||
328 | throw new \InvalidArgumentException( |
||
329 | "[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType" |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | 1 | foreach ($fields as $field) { |
|
334 | 1 | $this->addField($field, $modify); |
|
335 | 1 | } |
|
336 | |||
337 | 1 | return $this; |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * Remove field with specified name from the form. |
||
342 | * |
||
343 | * @param $name |
||
344 | * @return $this |
||
345 | */ |
||
346 | 2 | public function remove($name) |
|
347 | { |
||
348 | 2 | if ($this->has($name)) { |
|
349 | 2 | unset($this->fields[$name]); |
|
350 | 2 | } |
|
351 | |||
352 | 2 | return $this; |
|
353 | } |
||
354 | |||
355 | /** |
||
356 | * Modify existing field. If it doesn't exist, it is added to form. |
||
357 | * |
||
358 | * @param string $name |
||
359 | * @param string $type |
||
360 | * @param array $options |
||
361 | * @param bool $overwriteOptions |
||
362 | * @return Form |
||
363 | */ |
||
364 | 1 | public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
|
365 | { |
||
366 | // If we don't want to overwrite options, we merge them with old options. |
||
367 | 1 | if ($overwriteOptions === false && $this->has($name)) { |
|
368 | 1 | $options = $this->formHelper->mergeOptions( |
|
369 | 1 | $this->getField($name)->getOptions(), |
|
370 | $options |
||
371 | 1 | ); |
|
372 | 1 | } |
|
373 | |||
374 | 1 | return $this->add($name, $type, $options, true); |
|
375 | } |
||
376 | |||
377 | /** |
||
378 | * Render full form. |
||
379 | * |
||
380 | * @param array $options |
||
381 | * @param bool $showStart |
||
382 | * @param bool $showFields |
||
383 | * @param bool $showEnd |
||
384 | * @return string |
||
385 | */ |
||
386 | 7 | public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
|
390 | |||
391 | /** |
||
392 | * Render rest of the form. |
||
393 | * |
||
394 | * @param bool $showFormEnd |
||
395 | * @param bool $showFields |
||
396 | * @return string |
||
397 | */ |
||
398 | 1 | public function renderRest($showFormEnd = true, $showFields = true) |
|
404 | |||
405 | /** |
||
406 | * Renders the rest of the form up until the specified field name. |
||
407 | * |
||
408 | * @param string $field_name |
||
409 | * @param bool $showFormEnd |
||
410 | * @param bool $showFields |
||
411 | * @return string |
||
412 | */ |
||
413 | 2 | public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
|
414 | { |
||
415 | 2 | if (!$this->has($field_name)) { |
|
416 | 1 | $this->fieldDoesNotExist($field_name); |
|
417 | } |
||
418 | |||
419 | 1 | $fields = $this->getUnrenderedFields(); |
|
420 | |||
421 | 1 | $i = 1; |
|
422 | 1 | foreach ($fields as $key => $value) { |
|
423 | 1 | if ($value->getRealName() == $field_name) { |
|
424 | 1 | break; |
|
425 | } |
||
426 | 1 | $i++; |
|
427 | 1 | } |
|
428 | |||
429 | 1 | $fields = array_slice($fields, 0, $i, true); |
|
430 | |||
431 | 1 | return $this->render([], $fields, false, $showFields, $showFormEnd); |
|
432 | } |
||
433 | |||
434 | /** |
||
435 | * Get single field instance from form object. |
||
436 | * |
||
437 | * @param string $name |
||
438 | * @return FormField |
||
439 | */ |
||
440 | 35 | public function getField($name) |
|
448 | |||
449 | 101 | public function getErrorBag() |
|
453 | |||
454 | /** |
||
455 | * Check if form has field. |
||
456 | * |
||
457 | * @param string $name |
||
458 | * @return bool |
||
459 | */ |
||
460 | 61 | public function has($name) |
|
464 | |||
465 | /** |
||
466 | * Get all form options. |
||
467 | * |
||
468 | * @return array |
||
469 | */ |
||
470 | 2 | public function getFormOptions() |
|
474 | |||
475 | /** |
||
476 | * Get single form option. |
||
477 | * |
||
478 | * @param string $option |
||
479 | * @param mixed|null $default |
||
480 | * @return mixed |
||
481 | */ |
||
482 | 129 | public function getFormOption($option, $default = null) |
|
486 | |||
487 | /** |
||
488 | * Set single form option on form. |
||
489 | * |
||
490 | * @param string $option |
||
491 | * @param mixed $value |
||
492 | * |
||
493 | * @return $this |
||
494 | */ |
||
495 | 2 | public function setFormOption($option, $value) |
|
501 | |||
502 | /** |
||
503 | * Get the passed config key using the custom |
||
504 | * form config, if any. |
||
505 | * |
||
506 | * @param string $key |
||
507 | * @param mixed $default |
||
508 | * |
||
509 | * @return mixed |
||
510 | */ |
||
511 | 103 | public function getConfig($key = null, $default = null) |
|
515 | |||
516 | /** |
||
517 | * Set form options. |
||
518 | * |
||
519 | * @param array $formOptions |
||
520 | * @return $this |
||
521 | */ |
||
522 | 129 | public function setFormOptions(array $formOptions) |
|
536 | |||
537 | /** |
||
538 | * Get an option from provided options and call method with that value. |
||
539 | * |
||
540 | * @param string $name |
||
541 | * @param string $method |
||
542 | */ |
||
543 | 129 | protected function pullFromOptions($name, $method) |
|
544 | { |
||
545 | 129 | if (Arr::get($this->formOptions, $name) !== null) { |
|
546 | 20 | $this->{$method}(Arr::pull($this->formOptions, $name)); |
|
547 | 20 | } |
|
548 | 129 | } |
|
549 | |||
550 | /** |
||
551 | * Get form http method. |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | 3 | public function getMethod() |
|
559 | |||
560 | /** |
||
561 | * Set form http method. |
||
562 | * |
||
563 | * @param string $method |
||
564 | * @return $this |
||
565 | */ |
||
566 | 1 | public function setMethod($method) |
|
572 | |||
573 | /** |
||
574 | * Get form action url. |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | 3 | public function getUrl() |
|
582 | |||
583 | /** |
||
584 | * Set form action url. |
||
585 | * |
||
586 | * @param string $url |
||
587 | * @return $this |
||
588 | */ |
||
589 | 1 | public function setUrl($url) |
|
595 | |||
596 | /** |
||
597 | * Returns the name of the form. |
||
598 | * |
||
599 | * @return string|null |
||
600 | */ |
||
601 | 69 | public function getName() |
|
605 | |||
606 | /** |
||
607 | * Set the name of the form. |
||
608 | * |
||
609 | * @param string $name |
||
610 | * @param bool $rebuild |
||
611 | * @return $this |
||
612 | */ |
||
613 | 12 | public function setName($name, $rebuild = true) |
|
614 | { |
||
615 | 12 | $this->name = $name; |
|
616 | |||
617 | 12 | if ($rebuild) { |
|
618 | 12 | $this->rebuildForm(); |
|
619 | 12 | } |
|
620 | |||
621 | 12 | return $this; |
|
622 | } |
||
623 | |||
624 | /** |
||
625 | * Get model that is bind to form object. |
||
626 | * |
||
627 | * @return mixed |
||
628 | */ |
||
629 | 96 | public function getModel() |
|
633 | |||
634 | /** |
||
635 | * Set model to form object. |
||
636 | * |
||
637 | * @param mixed $model |
||
638 | * @return $this |
||
639 | * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
||
640 | */ |
||
641 | 17 | public function setModel($model) |
|
649 | |||
650 | /** |
||
651 | * Setup model for form, add namespace if needed for child forms. |
||
652 | * |
||
653 | * @return $this |
||
654 | */ |
||
655 | 12 | protected function setupModel($model) |
|
662 | |||
663 | /** |
||
664 | * Get all fields. |
||
665 | * |
||
666 | * @return FormField[] |
||
667 | */ |
||
668 | 129 | public function getFields() |
|
672 | |||
673 | /** |
||
674 | * Get field dynamically. |
||
675 | * |
||
676 | * @param string $name |
||
677 | * @return FormField |
||
678 | */ |
||
679 | 20 | public function __get($name) |
|
685 | |||
686 | /** |
||
687 | * Check if field exists when fetched using magic methods. |
||
688 | * |
||
689 | * @param string $name |
||
690 | * @return bool |
||
691 | */ |
||
692 | public function __isset($name) |
||
696 | |||
697 | /** |
||
698 | * Set the Event Dispatcher to fire Laravel events. |
||
699 | * |
||
700 | * @param EventDispatcher $eventDispatcher |
||
701 | * @return $this |
||
702 | */ |
||
703 | 129 | public function setEventDispatcher(EventDispatcher $eventDispatcher) |
|
709 | |||
710 | /** |
||
711 | * Set the form helper only on first instantiation. |
||
712 | * |
||
713 | * @param FormHelper $formHelper |
||
714 | * @return $this |
||
715 | */ |
||
716 | 129 | public function setFormHelper(FormHelper $formHelper) |
|
722 | |||
723 | /** |
||
724 | * Get form helper. |
||
725 | * |
||
726 | * @return FormHelper |
||
727 | */ |
||
728 | 101 | public function getFormHelper() |
|
732 | |||
733 | /** |
||
734 | * Add custom field. |
||
735 | * |
||
736 | * @param $name |
||
737 | * @param $class |
||
738 | */ |
||
739 | 2 | public function addCustomField($name, $class) |
|
747 | |||
748 | /** |
||
749 | * Returns wether form errors should be shown under every field. |
||
750 | * |
||
751 | * @return bool |
||
752 | */ |
||
753 | 101 | public function haveErrorsEnabled() |
|
757 | |||
758 | /** |
||
759 | * Enable or disable showing errors under fields |
||
760 | * |
||
761 | * @param bool $enabled |
||
762 | * @return $this |
||
763 | */ |
||
764 | 1 | public function setErrorsEnabled($enabled) |
|
770 | |||
771 | /** |
||
772 | * Is client validation enabled? |
||
773 | * |
||
774 | * @return bool |
||
775 | */ |
||
776 | 101 | public function clientValidationEnabled() |
|
780 | |||
781 | /** |
||
782 | * Enable/disable client validation. |
||
783 | * |
||
784 | * @param bool $enable |
||
785 | * @return $this |
||
786 | */ |
||
787 | 2 | public function setClientValidationEnabled($enable) |
|
793 | |||
794 | /** |
||
795 | * Add any aditional data that field needs (ex. array of choices). |
||
796 | * |
||
797 | * @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 |
||
798 | * will be switched to protected in 1.7. |
||
799 | * @param string $name |
||
800 | * @param mixed $data |
||
801 | */ |
||
802 | 1 | public function setData($name, $data) |
|
806 | |||
807 | /** |
||
808 | * Get single additional data. |
||
809 | * |
||
810 | * @param string $name |
||
811 | * @param null $default |
||
812 | * @return mixed |
||
813 | */ |
||
814 | 20 | public function getData($name = null, $default = null) |
|
822 | |||
823 | /** |
||
824 | * Add multiple peices of data at once. |
||
825 | * |
||
826 | * @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 |
||
827 | * will be switched to protected in 1.7. |
||
828 | * @param $data |
||
829 | * @return $this |
||
830 | **/ |
||
831 | 129 | public function addData(array $data) |
|
832 | { |
||
833 | 129 | foreach ($data as $key => $value) { |
|
834 | 1 | $this->setData($key, $value); |
|
835 | 129 | } |
|
836 | |||
837 | 129 | return $this; |
|
838 | } |
||
839 | |||
840 | /** |
||
841 | * Get current request. |
||
842 | * |
||
843 | * @return \Illuminate\Http\Request |
||
844 | */ |
||
845 | 101 | public function getRequest() |
|
849 | |||
850 | /** |
||
851 | * Set request on form. |
||
852 | * |
||
853 | * @param Request $request |
||
854 | * @return $this |
||
855 | */ |
||
856 | 129 | public function setRequest(Request $request) |
|
862 | |||
863 | /** |
||
864 | * Get template prefix that is prepended to all template paths. |
||
865 | * |
||
866 | * @return string |
||
867 | */ |
||
868 | 39 | public function getTemplatePrefix() |
|
876 | |||
877 | /** |
||
878 | * Set a template prefix for the form and its fields. |
||
879 | * |
||
880 | * @param string $prefix |
||
881 | * @return $this |
||
882 | */ |
||
883 | 4 | public function setTemplatePrefix($prefix) |
|
889 | |||
890 | /** |
||
891 | * Get the language name. |
||
892 | * |
||
893 | * @return string |
||
894 | */ |
||
895 | 98 | public function getLanguageName() |
|
899 | |||
900 | /** |
||
901 | * Set a language name, used as prefix for translated strings. |
||
902 | * |
||
903 | * @param string $prefix |
||
904 | * @return $this |
||
905 | */ |
||
906 | 13 | public function setLanguageName($prefix) |
|
912 | |||
913 | /** |
||
914 | * Get the translation template. |
||
915 | * |
||
916 | * @return string |
||
917 | */ |
||
918 | 100 | public function getTranslationTemplate() |
|
922 | |||
923 | /** |
||
924 | * Set a translation template, used to determine labels for fields. |
||
925 | * |
||
926 | * @param string $template |
||
927 | * @return $this |
||
928 | */ |
||
929 | 11 | public function setTranslationTemplate($template) |
|
935 | |||
936 | /** |
||
937 | * Render the form. |
||
938 | * |
||
939 | * @param array $options |
||
940 | * @param string $fields |
||
941 | * @param bool $showStart |
||
942 | * @param bool $showFields |
||
943 | * @param bool $showEnd |
||
944 | * @return string |
||
945 | */ |
||
946 | 9 | protected function render($options, $fields, $showStart, $showFields, $showEnd) |
|
962 | |||
963 | /** |
||
964 | * Get template from options if provided, otherwise fallback to config. |
||
965 | * |
||
966 | * @return mixed |
||
967 | */ |
||
968 | 9 | protected function getTemplate() |
|
972 | |||
973 | /** |
||
974 | * Get all fields that are not rendered. |
||
975 | * |
||
976 | * @return array |
||
977 | */ |
||
978 | 2 | protected function getUnrenderedFields() |
|
979 | { |
||
980 | 2 | $unrenderedFields = []; |
|
981 | |||
982 | 2 | foreach ($this->fields as $field) { |
|
983 | 2 | if (!$field->isRendered()) { |
|
984 | 2 | $unrenderedFields[] = $field; |
|
985 | 2 | continue; |
|
986 | } |
||
987 | 2 | } |
|
988 | |||
989 | 2 | return $unrenderedFields; |
|
990 | } |
||
991 | |||
992 | /** |
||
993 | * Prevent adding fields with same name. |
||
994 | * |
||
995 | * @param string $name |
||
996 | * @throws \InvalidArgumentException |
||
997 | * @return void |
||
998 | */ |
||
999 | 61 | protected function preventDuplicate($name) |
|
1005 | |||
1006 | /** |
||
1007 | * Returns and checks the type of the field. |
||
1008 | * |
||
1009 | * @param string $type |
||
1010 | * @return string |
||
1011 | */ |
||
1012 | 65 | protected function getFieldType($type) |
|
1018 | |||
1019 | /** |
||
1020 | * Check if form is named form. |
||
1021 | * |
||
1022 | * @return void |
||
1023 | */ |
||
1024 | 129 | protected function checkIfNamedForm() |
|
1025 | { |
||
1026 | 129 | if ($this->getFormOption('name')) { |
|
1027 | 8 | $this->name = Arr::pull($this->formOptions, 'name', $this->name); |
|
1028 | 8 | } |
|
1029 | 129 | } |
|
1030 | |||
1031 | /** |
||
1032 | * Set up options on single field depending on form options. |
||
1033 | * |
||
1034 | * @param string $name |
||
1035 | * @param $options |
||
1036 | */ |
||
1037 | 65 | protected function setupFieldOptions($name, &$options) |
|
1041 | |||
1042 | /** |
||
1043 | * Set namespace to model if form is named so the data is bound properly. |
||
1044 | * Returns true if model is changed, otherwise false. |
||
1045 | * |
||
1046 | * @return bool |
||
1047 | */ |
||
1048 | 21 | protected function setupNamedModel() |
|
1049 | { |
||
1050 | 21 | if (!$this->getModel() || !$this->getName()) { |
|
1051 | 19 | return false; |
|
1052 | } |
||
1053 | |||
1054 | 3 | $dotName = $this->formHelper->transformToDotSyntax($this->getName()); |
|
1055 | 3 | $model = $this->getModel(); |
|
1056 | 3 | $isCollectionFormModel = (bool) preg_match('/^.*\.\d+$/', $dotName); |
|
1057 | 3 | $isCollectionPrototype = strpos($dotName, '__NAME__') !== false; |
|
1058 | |||
1059 | 3 | if (!$this->formHelper->getModelValue($model, $dotName) && !$isCollectionFormModel && !$isCollectionPrototype) { |
|
1060 | 1 | $newModel = []; |
|
1061 | 1 | Arr::set($newModel, $dotName, $model); |
|
1062 | 1 | $this->model = $newModel; |
|
1063 | |||
1064 | 1 | if (is_object($model)) { |
|
1065 | 1 | $this->model = (object) $newModel; |
|
1066 | 1 | } |
|
1067 | |||
1068 | 1 | return true; |
|
1069 | } |
||
1070 | |||
1071 | 2 | return false; |
|
1072 | } |
||
1073 | |||
1074 | /** |
||
1075 | * Set form builder instance on helper so we can use it later. |
||
1076 | * |
||
1077 | * @param FormBuilder $formBuilder |
||
1078 | * @return $this |
||
1079 | */ |
||
1080 | 129 | public function setFormBuilder(FormBuilder $formBuilder) |
|
1086 | |||
1087 | /** |
||
1088 | * Returns the instance of the FormBuilder. |
||
1089 | * |
||
1090 | * @return FormBuilder |
||
1091 | */ |
||
1092 | 20 | public function getFormBuilder() |
|
1096 | |||
1097 | /** |
||
1098 | * Set the Validator instance on this so we can use it later. |
||
1099 | * |
||
1100 | * @param ValidatorFactory $validator |
||
1101 | * @return $this |
||
1102 | */ |
||
1103 | 129 | public function setValidator(ValidatorFactory $validator) |
|
1109 | |||
1110 | /** |
||
1111 | * Returns the validator instance. |
||
1112 | * |
||
1113 | * @return Validator |
||
1114 | */ |
||
1115 | 1 | public function getValidator() |
|
1119 | |||
1120 | /** |
||
1121 | * Exclude some fields from rendering. |
||
1122 | * |
||
1123 | * @return $this |
||
1124 | */ |
||
1125 | public function exclude(array $fields) |
||
1131 | |||
1132 | /** |
||
1133 | * If form is named form, modify names to be contained in single key (parent[child_field_name]). |
||
1134 | * |
||
1135 | * @param string $name |
||
1136 | * @return string |
||
1137 | */ |
||
1138 | 65 | protected function getFieldName($name) |
|
1139 | { |
||
1140 | 65 | $formName = $this->getName(); |
|
1141 | 65 | if ($formName !== null) { |
|
1142 | 14 | if (strpos($formName, '[') !== false || strpos($formName, ']') !== false) { |
|
1143 | 6 | return $this->formHelper->transformToBracketSyntax( |
|
1144 | 6 | $this->formHelper->transformToDotSyntax( |
|
1145 | 6 | $formName . '[' . $name . ']' |
|
1146 | 6 | ) |
|
1147 | 6 | ); |
|
1148 | } |
||
1149 | |||
1150 | 11 | return $formName . '[' . $name . ']'; |
|
1151 | } |
||
1152 | |||
1153 | 65 | return $name; |
|
1154 | } |
||
1155 | |||
1156 | /** |
||
1157 | * Disable all fields in a form. |
||
1158 | */ |
||
1159 | 1 | public function disableFields() |
|
1160 | { |
||
1161 | 1 | foreach ($this->fields as $field) { |
|
1162 | 1 | $field->disable(); |
|
1163 | 1 | } |
|
1164 | 1 | } |
|
1165 | |||
1166 | /** |
||
1167 | * Enable all fields in a form. |
||
1168 | */ |
||
1169 | 1 | public function enableFields() |
|
1170 | { |
||
1171 | 1 | foreach ($this->fields as $field) { |
|
1172 | 1 | $field->enable(); |
|
1173 | 1 | } |
|
1174 | 1 | } |
|
1175 | |||
1176 | /** |
||
1177 | * Validate the form. |
||
1178 | * |
||
1179 | * @param array $validationRules |
||
1180 | * @param array $messages |
||
1181 | * @return Validator |
||
1182 | */ |
||
1183 | 9 | public function validate($validationRules = [], $messages = []) |
|
1196 | |||
1197 | /** |
||
1198 | * Get validation rules for the form. |
||
1199 | * |
||
1200 | * @param array $overrideRules |
||
1201 | * @return array |
||
1202 | */ |
||
1203 | 1 | public function getRules($overrideRules = []) |
|
1209 | |||
1210 | /** |
||
1211 | * Redirects to a destination when form is invalid. |
||
1212 | * |
||
1213 | * @param string|null $destination The target url. |
||
1214 | * @return HttpResponseException |
||
1215 | */ |
||
1216 | 3 | public function redirectIfNotValid($destination = null) |
|
1217 | { |
||
1218 | 3 | if (! $this->isValid()) { |
|
1219 | 3 | $response = redirect($destination); |
|
1220 | |||
1221 | 3 | if (is_null($destination)) { |
|
1222 | 2 | $response = $response->back(); |
|
1223 | 2 | } |
|
1224 | |||
1225 | 3 | $response = $response->withErrors($this->getErrors(), $this->getErrorBag())->withInput(); |
|
1226 | |||
1227 | 3 | throw new HttpResponseException($response); |
|
1228 | } |
||
1229 | } |
||
1230 | |||
1231 | /** |
||
1232 | * Get all form field attributes, including child forms, in a flat array. |
||
1233 | * |
||
1234 | * @return array |
||
1235 | */ |
||
1236 | 3 | public function getAllAttributes() |
|
1240 | |||
1241 | /** |
||
1242 | * Check if the form is valid. |
||
1243 | * |
||
1244 | * @return bool |
||
1245 | */ |
||
1246 | 9 | public function isValid() |
|
1247 | { |
||
1248 | 9 | if (!$this->validator) { |
|
1249 | 8 | $this->validate(); |
|
1250 | 8 | } |
|
1251 | |||
1252 | 9 | $isValid = !$this->validator->fails(); |
|
1253 | |||
1254 | 9 | $this->formHelper->alterValid($this, $this, $isValid); |
|
1255 | |||
1256 | 9 | $this->eventDispatcher->dispatch(new AfterFormValidation($this, $this->validator, $isValid)); |
|
1257 | |||
1258 | 9 | return $isValid; |
|
1259 | } |
||
1260 | |||
1261 | /** |
||
1262 | * Optionally change the validation result, and/or add error messages. |
||
1263 | * |
||
1264 | * @param Form $mainForm |
||
1265 | * @param bool $isValid |
||
1266 | * @return void|array |
||
1267 | */ |
||
1268 | 9 | public function alterValid(Form $mainForm, &$isValid) |
|
1272 | |||
1273 | /** |
||
1274 | * Get validation errors. |
||
1275 | * |
||
1276 | * @return array |
||
1277 | */ |
||
1278 | 8 | public function getErrors() |
|
1279 | { |
||
1280 | 8 | if (!$this->validator || !$this->validator instanceof Validator) { |
|
1281 | 1 | throw new \InvalidArgumentException( |
|
1282 | 1 | sprintf( |
|
1283 | 1 | 'Form %s was not validated. To validate it, call "isValid" method before retrieving the errors', |
|
1284 | 1 | get_class($this) |
|
1285 | 1 | ) |
|
1286 | 1 | ); |
|
1287 | } |
||
1288 | |||
1289 | 7 | return $this->validator->getMessageBag()->getMessages(); |
|
1290 | } |
||
1291 | |||
1292 | /** |
||
1293 | * Get all Request values from all fields, and nothing else. |
||
1294 | * |
||
1295 | * @param bool $with_nulls |
||
1296 | * @return array |
||
1297 | */ |
||
1298 | 3 | public function getFieldValues($with_nulls = true) |
|
1299 | { |
||
1300 | 3 | $request_values = $this->getRequest()->all(); |
|
1301 | |||
1302 | 3 | $values = []; |
|
1303 | 3 | foreach ($this->getAllAttributes() as $attribute) { |
|
1304 | 3 | $value = Arr::get($request_values, $attribute); |
|
1305 | 3 | if ($with_nulls || $value !== null) { |
|
1306 | 3 | Arr::set($values, $attribute, $value); |
|
1307 | 3 | } |
|
1308 | 3 | } |
|
1309 | |||
1310 | // If this form is a child form, cherry pick a part |
||
1311 | 3 | if ($prefix = $this->getName()) { |
|
1312 | 1 | $prefix = $this->formHelper->transformToDotSyntax($prefix); |
|
1313 | 1 | $values = Arr::get($values, $prefix); |
|
1314 | 1 | } |
|
1315 | |||
1316 | // Allow form-specific value alters |
||
1317 | 3 | $this->formHelper->alterFieldValues($this, $values); |
|
1318 | |||
1319 | 3 | return $values; |
|
1320 | } |
||
1321 | |||
1322 | /** |
||
1323 | * Optionally mess with this form's $values before it's returned from getFieldValues(). |
||
1324 | * |
||
1325 | * @param array $values |
||
1326 | * @return void |
||
1327 | */ |
||
1328 | 3 | public function alterFieldValues(array &$values) |
|
1331 | |||
1332 | /** |
||
1333 | * Throw an exception indicating a field does not exist on the class. |
||
1334 | * |
||
1335 | * @param string $name |
||
1336 | * @throws \InvalidArgumentException |
||
1337 | * @return void |
||
1338 | */ |
||
1339 | 2 | protected function fieldDoesNotExist($name) |
|
1343 | |||
1344 | /** |
||
1345 | * Method filterFields used as *Main* method for starting |
||
1346 | * filtering and request field mutating process. |
||
1347 | * |
||
1348 | * @return \Kris\LaravelFormBuilder\Form |
||
1349 | */ |
||
1350 | 129 | public function filterFields() |
|
1351 | { |
||
1352 | // If filtering is unlocked/allowed we can start with filtering process. |
||
1353 | 129 | if (!$this->isFilteringLocked()) { |
|
1354 | 129 | $filters = array_filter($this->getFilters()); |
|
1355 | |||
1356 | 129 | if (count($filters)) { |
|
1357 | 1 | $dotForm = $this->formHelper->transformToDotSyntax($this->getName()); |
|
1358 | |||
1359 | 1 | $request = $this->getRequest(); |
|
1360 | 1 | $requestData = $request->all(); |
|
1361 | |||
1362 | 1 | foreach ($filters as $field => $fieldFilters) { |
|
1363 | 1 | $dotField = $this->formHelper->transformToDotSyntax($field); |
|
1364 | 1 | $fieldData = Arr::get($requestData, $dotField); |
|
1365 | 1 | if ($fieldData !== null) { |
|
1366 | // Assign current Raw/Unmutated value from request. |
||
1367 | 1 | $localDotField = preg_replace('#^' . preg_quote("$dotForm.", '#') . '#', '', $dotField); |
|
1368 | 1 | $localBracketField = $this->formHelper->transformToBracketSyntax($localDotField); |
|
1369 | 1 | $this->getField($localBracketField)->setRawValue($fieldData); |
|
1370 | 1 | foreach ($fieldFilters as $filter) { |
|
1371 | 1 | $filterObj = FilterResolver::instance($filter); |
|
1372 | 1 | $fieldData = $filterObj->filter($fieldData); |
|
1373 | 1 | } |
|
1374 | 1 | Arr::set($requestData, $dotField, $fieldData); |
|
1375 | 1 | } |
|
1376 | 1 | } |
|
1377 | |||
1378 | 1 | foreach ($requestData as $name => $value) { |
|
1379 | 1 | $request[$name] = $value; |
|
1380 | 1 | } |
|
1381 | 1 | } |
|
1382 | 129 | } |
|
1383 | |||
1384 | 129 | return $this; |
|
1385 | } |
||
1386 | |||
1387 | /** |
||
1388 | * Method getFilters used to return array of all binded filters to form fields. |
||
1389 | * |
||
1390 | * @return array |
||
1391 | */ |
||
1392 | 129 | public function getFilters() |
|
1393 | { |
||
1394 | 129 | $filters = []; |
|
1395 | 129 | foreach ($this->getFields() as $field) { |
|
1396 | 17 | $filters[$field->getName()] = $field->getFilters(); |
|
1397 | 129 | } |
|
1398 | |||
1399 | 129 | return $filters; |
|
1400 | } |
||
1401 | |||
1402 | /** |
||
1403 | * If lockFiltering is set to true then we will not |
||
1404 | * filter fields and mutate request data binded to fields. |
||
1405 | * |
||
1406 | * @return \Kris\LaravelFormBuilder\Form |
||
1407 | */ |
||
1408 | 1 | public function lockFiltering() |
|
1413 | |||
1414 | /** |
||
1415 | * Unlock fields filtering/mutating. |
||
1416 | * |
||
1417 | * @return \Kris\LaravelFormBuilder\Form |
||
1418 | */ |
||
1419 | public function unlockFiltering() |
||
1424 | |||
1425 | /** |
||
1426 | * Method isFilteringLocked used to check |
||
1427 | * if current filteringLocked property status is set to true. |
||
1428 | * |
||
1429 | * @return bool |
||
1430 | */ |
||
1431 | 129 | public function isFilteringLocked() |
|
1435 | |||
1436 | /** |
||
1437 | * Method getRawValues returns Unfiltered/Unmutated fields -> values. |
||
1438 | * |
||
1439 | * @return array |
||
1440 | */ |
||
1441 | public function getRawValues() |
||
1442 | { |
||
1443 | $rawValues = []; |
||
1444 | foreach ($this->getFields() as $field) { |
||
1445 | $rawValues[$field->getName()] = $field->getRawValue(); |
||
1450 | } |
||
1451 |
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: