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 | // 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() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Rebuild the form from scratch. |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | 19 | public function rebuildForm() |
|
| 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 = []) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 479 | |||
| 480 | 101 | public function getErrorBag() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Check if form has field. |
||
| 487 | * |
||
| 488 | * @param string $name |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | 61 | public function has($name) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get all form options. |
||
| 498 | * |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | 2 | public function getFormOptions() |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Set form options. |
||
| 549 | * |
||
| 550 | * @param array $formOptions |
||
| 551 | * @return $this |
||
| 552 | */ |
||
| 553 | 129 | public function setFormOptions(array $formOptions) |
|
| 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) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Get form http method. |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | 3 | public function getMethod() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Set form http method. |
||
| 593 | * |
||
| 594 | * @param string $method |
||
| 595 | * @return $this |
||
| 596 | */ |
||
| 597 | 1 | public function setMethod($method) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Get form action url. |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | 3 | public function getUrl() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Set form action url. |
||
| 616 | * |
||
| 617 | * @param string $url |
||
| 618 | * @return $this |
||
| 619 | */ |
||
| 620 | 1 | public function setUrl($url) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Returns the name of the form. |
||
| 629 | * |
||
| 630 | * @return string|null |
||
| 631 | */ |
||
| 632 | 69 | public function getName() |
|
| 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) |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Get model that is bind to form object. |
||
| 657 | * |
||
| 658 | * @return mixed |
||
| 659 | */ |
||
| 660 | 96 | public function getModel() |
|
| 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) |
|
| 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) |
|
| 693 | |||
| 694 | /** |
||
| 695 | * Get all fields. |
||
| 696 | * |
||
| 697 | * @return FormField[] |
||
| 698 | */ |
||
| 699 | 129 | public function getFields() |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Get field dynamically. |
||
| 706 | * |
||
| 707 | * @param string $name |
||
| 708 | * @return FormField |
||
| 709 | */ |
||
| 710 | 20 | public function __get($name) |
|
| 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) |
||
| 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) |
|
| 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) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Get form helper. |
||
| 756 | * |
||
| 757 | * @return FormHelper |
||
| 758 | */ |
||
| 759 | 101 | public function getFormHelper() |
|
| 763 | |||
| 764 | /** |
||
| 765 | * Add custom field. |
||
| 766 | * |
||
| 767 | * @param $name |
||
| 768 | * @param $class |
||
| 769 | */ |
||
| 770 | 2 | public function addCustomField($name, $class) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Returns wether form errors should be shown under every field. |
||
| 781 | * |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | 101 | public function haveErrorsEnabled() |
|
| 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) |
|
| 801 | |||
| 802 | /** |
||
| 803 | * Is client validation enabled? |
||
| 804 | * |
||
| 805 | * @return bool |
||
| 806 | */ |
||
| 807 | 101 | public function clientValidationEnabled() |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Enable/disable client validation. |
||
| 814 | * |
||
| 815 | * @param bool $enable |
||
| 816 | * @return $this |
||
| 817 | */ |
||
| 818 | 2 | public function setClientValidationEnabled($enable) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Get current request. |
||
| 873 | * |
||
| 874 | * @return \Illuminate\Http\Request |
||
| 875 | */ |
||
| 876 | 129 | public function getRequest() |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Set request on form. |
||
| 883 | * |
||
| 884 | * @param Request $request |
||
| 885 | * @return $this |
||
| 886 | */ |
||
| 887 | 129 | public function setRequest(Request $request) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * Get template prefix that is prepended to all template paths. |
||
| 896 | * |
||
| 897 | * @return string |
||
| 898 | */ |
||
| 899 | 39 | public function getTemplatePrefix() |
|
| 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) |
|
| 920 | |||
| 921 | /** |
||
| 922 | * Get the language name. |
||
| 923 | * |
||
| 924 | * @return string |
||
| 925 | */ |
||
| 926 | 98 | public function getLanguageName() |
|
| 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) |
|
| 943 | |||
| 944 | /** |
||
| 945 | * Get the translation template. |
||
| 946 | * |
||
| 947 | * @return string |
||
| 948 | */ |
||
| 949 | 100 | public function getTranslationTemplate() |
|
| 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) |
|
| 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) |
|
| 993 | |||
| 994 | /** |
||
| 995 | * Get template from options if provided, otherwise fallback to config. |
||
| 996 | * |
||
| 997 | * @return mixed |
||
| 998 | */ |
||
| 999 | 9 | protected function getTemplate() |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Get all fields that are not rendered. |
||
| 1006 | * |
||
| 1007 | * @return array |
||
| 1008 | */ |
||
| 1009 | 2 | protected function getUnrenderedFields() |
|
| 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) |
|
| 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) |
|
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Check if form is named form. |
||
| 1052 | * |
||
| 1053 | * @return void |
||
| 1054 | */ |
||
| 1055 | 129 | protected function checkIfNamedForm() |
|
| 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) |
|
| 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() |
|
| 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) |
|
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Returns the instance of the FormBuilder. |
||
| 1116 | * |
||
| 1117 | * @return FormBuilder |
||
| 1118 | */ |
||
| 1119 | 12 | public function getFormBuilder() |
|
| 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) |
|
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Returns the validator instance. |
||
| 1139 | * |
||
| 1140 | * @return Validator |
||
| 1141 | */ |
||
| 1142 | 1 | public function getValidator() |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Exclude some fields from rendering. |
||
| 1149 | * |
||
| 1150 | * @return $this |
||
| 1151 | */ |
||
| 1152 | public function exclude(array $fields) |
||
| 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) |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Disable all fields in a form. |
||
| 1185 | */ |
||
| 1186 | 1 | public function disableFields() |
|
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Enable all fields in a form. |
||
| 1195 | */ |
||
| 1196 | 1 | public function enableFields() |
|
| 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 = []) |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Get validation rules for the form. |
||
| 1226 | * |
||
| 1227 | * @param array $overrideRules |
||
| 1228 | * @return array |
||
| 1229 | */ |
||
| 1230 | 1 | public function getRules($overrideRules = []) |
|
| 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) |
|
| 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() |
|
| 1267 | |||
| 1268 | /** |
||
| 1269 | * Check if the form is valid. |
||
| 1270 | * |
||
| 1271 | * @return bool |
||
| 1272 | */ |
||
| 1273 | 9 | public function isValid() |
|
| 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) |
|
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Get validation errors. |
||
| 1302 | * |
||
| 1303 | * @return array |
||
| 1304 | */ |
||
| 1305 | 8 | public function getErrors() |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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() |
|
| 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() |
|
| 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() |
|
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Unlock fields filtering/mutating. |
||
| 1433 | * |
||
| 1434 | * @return \Kris\LaravelFormBuilder\Form |
||
| 1435 | */ |
||
| 1436 | public function unlockFiltering() |
||
| 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() |
|
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Method getRawValues returns Unfiltered/Unmutated fields -> values. |
||
| 1455 | * |
||
| 1456 | * @return array |
||
| 1457 | */ |
||
| 1458 | public function getRawValues() |
||
| 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: