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 |
||
| 16 | class Form |
||
| 17 | { |
||
| 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 | * Build the form. |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | 3 | public function buildForm() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Rebuild the form from scratch. |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | 17 | public function rebuildForm() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Create the FormField object. |
||
| 160 | * |
||
| 161 | * @param string $name |
||
| 162 | * @param string $type |
||
| 163 | * @param array $options |
||
| 164 | * @return FormField |
||
| 165 | */ |
||
| 166 | 51 | protected function makeField($name, $type = 'text', array $options = []) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Create a new field and add it to the form. |
||
| 183 | * |
||
| 184 | * @param string $name |
||
| 185 | * @param string $type |
||
| 186 | * @param array $options |
||
| 187 | * @param bool $modify |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | 53 | public function add($name, $type = 'text', array $options = [], $modify = false) |
|
| 191 | { |
||
| 192 | 53 | $this->formHelper->checkFieldName($name, get_class($this)); |
|
| 193 | |||
| 194 | 51 | if ($this->rebuilding && !$this->has($name)) { |
|
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | 51 | $this->addField($this->makeField($name, $type, $options), $modify); |
|
| 199 | |||
| 200 | 47 | return $this; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Add a FormField to the form's fields. |
||
| 205 | * |
||
| 206 | * @param FormField $field |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | 47 | protected function addField(FormField $field, $modify = false) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Add field before another field. |
||
| 227 | * |
||
| 228 | * @param string $name Name of the field before which new field is added. |
||
| 229 | * @param string $fieldName Field name which will be added. |
||
| 230 | * @param string $type |
||
| 231 | * @param array $options |
||
| 232 | * @param bool $modify |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Add field before another field. |
||
| 253 | * |
||
| 254 | * @param string $name Name of the field after 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 addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Take another form and add it's fields directly to this form. |
||
| 279 | * |
||
| 280 | * @param mixed $class Form to merge. |
||
| 281 | * @param array $options |
||
| 282 | * @param boolean $modify |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 1 | public function compose($class, array $options = [], $modify = false) |
|
| 286 | { |
||
| 287 | 1 | $options['class'] = $class; |
|
| 288 | |||
| 289 | // If we pass a ready made form just extract the fields. |
||
| 290 | 1 | if ($class instanceof Form) { |
|
| 291 | 1 | $fields = $class->getFields(); |
|
| 292 | } elseif ($class instanceof Fields\ChildFormType) { |
||
| 293 | $fields = $class->getForm()->getFields(); |
||
| 294 | } elseif (is_string($class)) { |
||
| 295 | // If its a string of a class make it the usual way. |
||
| 296 | $options['model'] = $this->model; |
||
| 297 | $options['name'] = $this->name; |
||
| 298 | |||
| 299 | $form = $this->formBuilder->create($class, $options); |
||
| 300 | $fields = $form->getFields(); |
||
| 301 | } else { |
||
| 302 | throw new \InvalidArgumentException( |
||
| 303 | "[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType" |
||
| 304 | ); |
||
| 305 | } |
||
| 306 | |||
| 307 | 1 | foreach ($fields as $field) { |
|
| 308 | 1 | $this->addField($field, $modify); |
|
| 309 | } |
||
| 310 | |||
| 311 | 1 | return $this; |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Remove field with specified name from the form. |
||
| 316 | * |
||
| 317 | * @param $name |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | 2 | public function remove($name) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Modify existing field. If it doesn't exist, it is added to form. |
||
| 331 | * |
||
| 332 | * @param string $name |
||
| 333 | * @param string $type |
||
| 334 | * @param array $options |
||
| 335 | * @param bool $overwriteOptions |
||
| 336 | * @return Form |
||
| 337 | */ |
||
| 338 | 1 | public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Render full form. |
||
| 353 | * |
||
| 354 | * @param array $options |
||
| 355 | * @param bool $showStart |
||
| 356 | * @param bool $showFields |
||
| 357 | * @param bool $showEnd |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 7 | public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Render rest of the form. |
||
| 367 | * |
||
| 368 | * @param bool $showFormEnd |
||
| 369 | * @param bool $showFields |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 1 | public function renderRest($showFormEnd = true, $showFields = true) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Renders the rest of the form up until the specified field name. |
||
| 381 | * |
||
| 382 | * @param string $field_name |
||
| 383 | * @param bool $showFormEnd |
||
| 384 | * @param bool $showFields |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | 2 | public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get single field instance from form object. |
||
| 410 | * |
||
| 411 | * @param string $name |
||
| 412 | * @return FormField |
||
| 413 | */ |
||
| 414 | 25 | public function getField($name) |
|
| 415 | { |
||
| 416 | 25 | if ($this->has($name)) { |
|
| 417 | 24 | return $this->fields[$name]; |
|
| 418 | } |
||
| 419 | |||
| 420 | 1 | $this->fieldDoesNotExist($name); |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Check if form has field. |
||
| 425 | * |
||
| 426 | * @param string $name |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | 47 | public function has($name) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get all form options. |
||
| 436 | * |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | 2 | public function getFormOptions() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Get single form option. |
||
| 446 | * |
||
| 447 | * @param string $option |
||
| 448 | * @param mixed|null $default |
||
| 449 | * @return mixed |
||
| 450 | */ |
||
| 451 | 107 | public function getFormOption($option, $default = null) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Set single form option on form. |
||
| 458 | * |
||
| 459 | * @param string $option |
||
| 460 | * @param mixed $value |
||
| 461 | * |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | 2 | public function setFormOption($option, $value) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Set form options. |
||
| 473 | * |
||
| 474 | * @param array $formOptions |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | 107 | public function setFormOptions(array $formOptions) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get an option from provided options and call method with that value. |
||
| 493 | * |
||
| 494 | * @param string $name |
||
| 495 | * @param string $method |
||
| 496 | */ |
||
| 497 | 107 | protected function pullFromOptions($name, $method) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Get form http method. |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | 3 | public function getMethod() |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Set form http method. |
||
| 516 | * |
||
| 517 | * @param string $method |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | 1 | public function setMethod($method) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Get form action url. |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | 3 | public function getUrl() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Set form action url. |
||
| 539 | * |
||
| 540 | * @param string $url |
||
| 541 | * @return $this |
||
| 542 | */ |
||
| 543 | 1 | public function setUrl($url) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Returns the name of the form. |
||
| 552 | * |
||
| 553 | * @return string|null |
||
| 554 | */ |
||
| 555 | 52 | public function getName() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Set the name of the form. |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * @param bool $rebuild |
||
| 565 | * @return $this |
||
| 566 | */ |
||
| 567 | 10 | public function setName($name, $rebuild = true) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Get model that is bind to form object. |
||
| 580 | * |
||
| 581 | * @return mixed |
||
| 582 | */ |
||
| 583 | 81 | public function getModel() |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Set model to form object. |
||
| 590 | * |
||
| 591 | * @param mixed $model |
||
| 592 | * @return $this |
||
| 593 | * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
||
| 594 | */ |
||
| 595 | 15 | public function setModel($model) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Setup model for form, add namespace if needed for child forms. |
||
| 606 | * |
||
| 607 | * @return $this |
||
| 608 | */ |
||
| 609 | 12 | protected function setupModel($model) |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Get all fields. |
||
| 618 | * |
||
| 619 | * @return FormField[] |
||
| 620 | */ |
||
| 621 | 29 | public function getFields() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Get field dynamically. |
||
| 628 | * |
||
| 629 | * @param string $name |
||
| 630 | * @return FormField |
||
| 631 | */ |
||
| 632 | 19 | public function __get($name) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Check if field exists when fetched using magic methods. |
||
| 641 | * |
||
| 642 | * @param string $name |
||
| 643 | * @return bool |
||
| 644 | */ |
||
| 645 | public function __isset($name) |
||
| 646 | { |
||
| 647 | return $this->has($name); |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Set the Event Dispatcher to fire Laravel events. |
||
| 652 | * |
||
| 653 | * @param EventDispatcher $eventDispatcher |
||
| 654 | * @return $this |
||
| 655 | */ |
||
| 656 | 107 | public function setEventDispatcher(EventDispatcher $eventDispatcher) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Set the form helper only on first instantiation. |
||
| 665 | * |
||
| 666 | * @param FormHelper $formHelper |
||
| 667 | * @return $this |
||
| 668 | */ |
||
| 669 | 107 | public function setFormHelper(FormHelper $formHelper) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Get form helper. |
||
| 678 | * |
||
| 679 | * @return FormHelper |
||
| 680 | */ |
||
| 681 | 85 | public function getFormHelper() |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Add custom field. |
||
| 688 | * |
||
| 689 | * @param $name |
||
| 690 | * @param $class |
||
| 691 | */ |
||
| 692 | 2 | public function addCustomField($name, $class) |
|
| 696 | |||
| 697 | /** |
||
| 698 | * Returns wether form errors should be shown under every field. |
||
| 699 | * |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | 85 | public function haveErrorsEnabled() |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Enable or disable showing errors under fields |
||
| 709 | * |
||
| 710 | * @param bool $enabled |
||
| 711 | * @return $this |
||
| 712 | */ |
||
| 713 | 1 | public function setErrorsEnabled($enabled) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Is client validation enabled? |
||
| 722 | * |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | 21 | public function clientValidationEnabled() |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Enable/disable client validation. |
||
| 732 | * |
||
| 733 | * @param bool $enable |
||
| 734 | * @return $this |
||
| 735 | */ |
||
| 736 | 2 | public function setClientValidationEnabled($enable) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Add any aditional data that field needs (ex. array of choices). |
||
| 745 | * |
||
| 746 | * @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 |
||
| 747 | * will be switched to protected in 1.7. |
||
| 748 | * @param string $name |
||
| 749 | * @param mixed $data |
||
| 750 | */ |
||
| 751 | 1 | public function setData($name, $data) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Get single additional data. |
||
| 758 | * |
||
| 759 | * @param string $name |
||
| 760 | * @param null $default |
||
| 761 | * @return mixed |
||
| 762 | */ |
||
| 763 | 18 | public function getData($name = null, $default = null) |
|
| 771 | |||
| 772 | /** |
||
| 773 | * Add multiple peices of data at once. |
||
| 774 | * |
||
| 775 | * @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 |
||
| 776 | * will be switched to protected in 1.7. |
||
| 777 | * @param $data |
||
| 778 | * @return $this |
||
| 779 | **/ |
||
| 780 | 107 | public function addData(array $data) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Get current request. |
||
| 791 | * |
||
| 792 | * @return \Illuminate\Http\Request |
||
| 793 | */ |
||
| 794 | 85 | public function getRequest() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Set request on form. |
||
| 801 | * |
||
| 802 | * @param Request $request |
||
| 803 | * @return $this |
||
| 804 | */ |
||
| 805 | 107 | public function setRequest(Request $request) |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Get template prefix that is prepended to all template paths. |
||
| 814 | * |
||
| 815 | * @return string |
||
| 816 | */ |
||
| 817 | 37 | public function getTemplatePrefix() |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Set a template prefix for the form and its fields. |
||
| 828 | * |
||
| 829 | * @param string $prefix |
||
| 830 | * @return $this |
||
| 831 | */ |
||
| 832 | 4 | public function setTemplatePrefix($prefix) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Get the language name. |
||
| 841 | * |
||
| 842 | * @return string |
||
| 843 | */ |
||
| 844 | 83 | public function getLanguageName() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Set a language name, used as prefix for translated strings. |
||
| 851 | * |
||
| 852 | * @param string $prefix |
||
| 853 | * @return $this |
||
| 854 | */ |
||
| 855 | 11 | public function setLanguageName($prefix) |
|
| 861 | |||
| 862 | /** |
||
| 863 | * Render the form. |
||
| 864 | * |
||
| 865 | * @param array $options |
||
| 866 | * @param string $fields |
||
| 867 | * @param bool $showStart |
||
| 868 | * @param bool $showFields |
||
| 869 | * @param bool $showEnd |
||
| 870 | * @return string |
||
| 871 | */ |
||
| 872 | 9 | protected function render($options, $fields, $showStart, $showFields, $showEnd) |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Get template from options if provided, otherwise fallback to config. |
||
| 891 | * |
||
| 892 | * @return mixed |
||
| 893 | */ |
||
| 894 | 9 | protected function getTemplate() |
|
| 898 | |||
| 899 | /** |
||
| 900 | * Get all fields that are not rendered. |
||
| 901 | * |
||
| 902 | * @return array |
||
| 903 | */ |
||
| 904 | 2 | protected function getUnrenderedFields() |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Prevent adding fields with same name. |
||
| 920 | * |
||
| 921 | * @param string $name |
||
| 922 | * @throws \InvalidArgumentException |
||
| 923 | * @return void |
||
| 924 | */ |
||
| 925 | 47 | protected function preventDuplicate($name) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Returns and checks the type of the field. |
||
| 934 | * |
||
| 935 | * @param string $type |
||
| 936 | * @return string |
||
| 937 | */ |
||
| 938 | 51 | protected function getFieldType($type) |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Check if form is named form. |
||
| 947 | * |
||
| 948 | * @return void |
||
| 949 | */ |
||
| 950 | 107 | protected function checkIfNamedForm() |
|
| 956 | |||
| 957 | /** |
||
| 958 | * Set up options on single field depending on form options. |
||
| 959 | * |
||
| 960 | * @param string $name |
||
| 961 | * @param $options |
||
| 962 | */ |
||
| 963 | 51 | protected function setupFieldOptions($name, &$options) |
|
| 967 | |||
| 968 | /** |
||
| 969 | * Set namespace to model if form is named so the data is bound properly. |
||
| 970 | * Returns true if model is changed, otherwise false. |
||
| 971 | * |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | 9 | protected function setupNamedModel() |
|
| 975 | { |
||
| 976 | 9 | if (!$this->getModel() || !$this->getName()) { |
|
| 977 | 8 | return false; |
|
| 978 | } |
||
| 979 | |||
| 980 | 1 | $dotName = $this->formHelper->transformToDotSyntax($this->getName()); |
|
| 981 | 1 | $model = $this->formHelper->convertModelToArray($this->getModel()); |
|
| 982 | |||
| 983 | 1 | if (!array_get($model, $dotName)) { |
|
| 984 | 1 | $newModel = []; |
|
| 985 | 1 | array_set($newModel, $dotName, $model); |
|
| 986 | 1 | $this->model = $newModel; |
|
| 987 | |||
| 988 | 1 | return true; |
|
| 989 | } |
||
| 990 | |||
| 991 | return false; |
||
| 992 | } |
||
| 993 | |||
| 994 | |||
| 995 | /** |
||
| 996 | * Set form builder instance on helper so we can use it later. |
||
| 997 | * |
||
| 998 | * @param FormBuilder $formBuilder |
||
| 999 | * @return $this |
||
| 1000 | */ |
||
| 1001 | 107 | public function setFormBuilder(FormBuilder $formBuilder) |
|
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Returns the instance of the FormBuilder. |
||
| 1010 | * |
||
| 1011 | * @return FormBuilder |
||
| 1012 | */ |
||
| 1013 | 12 | public function getFormBuilder() |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Set the Validator instance on this so we can use it later. |
||
| 1020 | * |
||
| 1021 | * @param ValidatorFactory $validator |
||
| 1022 | * @return $this |
||
| 1023 | */ |
||
| 1024 | 107 | public function setValidator(ValidatorFactory $validator) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Returns the validator instance. |
||
| 1033 | * |
||
| 1034 | * @return Validator |
||
| 1035 | */ |
||
| 1036 | 1 | public function getValidator() |
|
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Exclude some fields from rendering. |
||
| 1043 | * |
||
| 1044 | * @return $this |
||
| 1045 | */ |
||
| 1046 | public function exclude(array $fields) |
||
| 1047 | { |
||
| 1048 | $this->exclude = array_merge($this->exclude, $fields); |
||
| 1049 | |||
| 1050 | return $this; |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * If form is named form, modify names to be contained in single key (parent[child_field_name]). |
||
| 1056 | * |
||
| 1057 | * @param string $name |
||
| 1058 | * @return string |
||
| 1059 | */ |
||
| 1060 | 51 | protected function getFieldName($name) |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Disable all fields in a form. |
||
| 1080 | */ |
||
| 1081 | 1 | public function disableFields() |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Enable all fields in a form. |
||
| 1090 | */ |
||
| 1091 | 1 | public function enableFields() |
|
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Validate the form. |
||
| 1100 | * |
||
| 1101 | * @param array $validationRules |
||
| 1102 | * @param array $messages |
||
| 1103 | * @return Validator |
||
| 1104 | */ |
||
| 1105 | 8 | public function validate($validationRules = [], $messages = []) |
|
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Get validation rules for the form. |
||
| 1121 | * |
||
| 1122 | * @param array $overrideRules |
||
| 1123 | * @return array |
||
| 1124 | */ |
||
| 1125 | 1 | public function getRules($overrideRules = []) |
|
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Redirects to a destination when form is invalid. |
||
| 1134 | * |
||
| 1135 | * @param string|null $destination The target url. |
||
| 1136 | * @return HttpResponseException |
||
| 1137 | */ |
||
| 1138 | 2 | public function redirectIfNotValid($destination = null) |
|
| 1139 | { |
||
| 1140 | 2 | if (! $this->isValid()) { |
|
| 1141 | 2 | $response = redirect($destination); |
|
| 1142 | |||
| 1143 | 2 | if (is_null($destination)) { |
|
| 1144 | 1 | $response = $response->back(); |
|
| 1145 | } |
||
| 1146 | |||
| 1147 | 2 | $response = $response->withErrors($this->getErrors())->withInput(); |
|
| 1148 | |||
| 1149 | 2 | throw new HttpResponseException($response); |
|
| 1150 | } |
||
| 1151 | } |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Get all form field attributes, including child forms, in a flat array. |
||
| 1155 | * |
||
| 1156 | * @return array |
||
| 1157 | */ |
||
| 1158 | 3 | public function getAllAttributes() |
|
| 1162 | |||
| 1163 | /** |
||
| 1164 | * Check if the form is valid. |
||
| 1165 | * |
||
| 1166 | * @return bool |
||
| 1167 | */ |
||
| 1168 | 8 | public function isValid() |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Optionally change the validation result, and/or add error messages. |
||
| 1185 | * |
||
| 1186 | * @param Form $mainForm |
||
| 1187 | * @param bool $isValid |
||
| 1188 | * @return void|array |
||
| 1189 | */ |
||
| 1190 | 8 | public function alterValid(Form $mainForm, &$isValid) |
|
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Get validation errors. |
||
| 1197 | * |
||
| 1198 | * @return array |
||
| 1199 | */ |
||
| 1200 | 7 | public function getErrors() |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Get all Request values from all fields, and nothing else. |
||
| 1216 | * |
||
| 1217 | * @param bool $with_nulls |
||
| 1218 | * @return array |
||
| 1219 | */ |
||
| 1220 | 3 | public function getFieldValues($with_nulls = true) |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Optionally mess with this form's $values before it's returned from getFieldValues(). |
||
| 1246 | * |
||
| 1247 | * @param array $values |
||
| 1248 | * @return void |
||
| 1249 | */ |
||
| 1250 | 3 | public function alterFieldValues(array &$values) |
|
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Throw an exception indicating a field does not exist on the class. |
||
| 1256 | * |
||
| 1257 | * @param string $name |
||
| 1258 | * @throws \InvalidArgumentException |
||
| 1259 | * @return void |
||
| 1260 | */ |
||
| 1261 | 2 | protected function fieldDoesNotExist($name) |
|
| 1265 | } |
||
| 1266 |
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: