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 | * Build the form. |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | */ |
||
| 149 | 3 | public function buildForm() |
|
| 150 | { |
||
| 151 | 3 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Rebuild the form from scratch. |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | 27 | public function rebuildForm() |
|
| 159 | { |
||
| 160 | 27 | $this->rebuilding = true; |
|
| 161 | // If form is plain, buildForm method is empty, so we need to take |
||
| 162 | // existing fields and add them again |
||
| 163 | 27 | if (get_class($this) === 'Kris\LaravelFormBuilder\Form') { |
|
| 164 | 24 | foreach ($this->fields as $name => $field) { |
|
| 165 | // Remove any temp variables added in previous instance |
||
| 166 | 13 | $options = array_except($field->getOptions(), 'tmp'); |
|
| 167 | 24 | $this->add($name, $field->getType(), $options); |
|
| 168 | } |
||
| 169 | } else { |
||
| 170 | 5 | $this->buildForm(); |
|
| 171 | } |
||
| 172 | 27 | $this->rebuilding = false; |
|
| 173 | |||
| 174 | 27 | return $this; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Create the FormField object. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * @param string $type |
||
| 182 | * @param array $options |
||
| 183 | * @return FormField |
||
| 184 | */ |
||
| 185 | 65 | protected function makeField($name, $type = 'text', array $options = []) |
|
| 186 | { |
||
| 187 | 65 | $this->setupFieldOptions($name, $options); |
|
| 188 | |||
| 189 | 65 | $fieldName = $this->getFieldName($name); |
|
| 190 | |||
| 191 | 65 | $fieldType = $this->getFieldType($type); |
|
| 192 | |||
| 193 | 64 | $field = new $fieldType($fieldName, $type, $this, $options); |
|
| 194 | |||
| 195 | 61 | $this->eventDispatcher->fire(new AfterFieldCreation($this, $field)); |
|
| 196 | |||
| 197 | 61 | return $field; |
|
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Create a new field and add it to the form. |
||
| 202 | * |
||
| 203 | * @param string $name |
||
| 204 | * @param string $type |
||
| 205 | * @param array $options |
||
| 206 | * @param bool $modify |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | 67 | public function add($name, $type = 'text', array $options = [], $modify = false) |
|
| 210 | { |
||
| 211 | 67 | $this->formHelper->checkFieldName($name, get_class($this)); |
|
| 212 | |||
| 213 | 65 | if ($this->rebuilding && !$this->has($name)) { |
|
| 214 | return $this; |
||
| 215 | } |
||
| 216 | |||
| 217 | 65 | $this->addField($this->makeField($name, $type, $options), $modify); |
|
| 218 | |||
| 219 | 61 | return $this; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Add a FormField to the form's fields. |
||
| 224 | * |
||
| 225 | * @param FormField $field |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | 61 | protected function addField(FormField $field, $modify = false) |
|
| 229 | { |
||
| 230 | 61 | if (!$modify && !$this->rebuilding) { |
|
| 231 | 61 | $this->preventDuplicate($field->getRealName()); |
|
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | 61 | if ($field->getType() == 'file') { |
|
| 236 | 3 | $this->formOptions['files'] = true; |
|
| 237 | } |
||
| 238 | |||
| 239 | 61 | $this->fields[$field->getRealName()] = $field; |
|
| 240 | |||
| 241 | 61 | return $this; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Add field before another field. |
||
| 246 | * |
||
| 247 | * @param string $name Name of the field before which new field is added. |
||
| 248 | * @param string $fieldName Field name which will be added. |
||
| 249 | * @param string $type |
||
| 250 | * @param array $options |
||
| 251 | * @param bool $modify |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | 1 | public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
| 255 | { |
||
| 256 | 1 | $offset = array_search($name, array_keys($this->fields)); |
|
| 257 | |||
| 258 | 1 | $beforeFields = array_slice($this->fields, 0, $offset); |
|
| 259 | 1 | $afterFields = array_slice($this->fields, $offset); |
|
| 260 | |||
| 261 | 1 | $this->fields = $beforeFields; |
|
| 262 | |||
| 263 | 1 | $this->add($fieldName, $type, $options, $modify); |
|
| 264 | |||
| 265 | 1 | $this->fields += $afterFields; |
|
| 266 | |||
| 267 | 1 | return $this; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Add field before another field. |
||
| 272 | * |
||
| 273 | * @param string $name Name of the field after which new field is added. |
||
| 274 | * @param string $fieldName Field name which will be added. |
||
| 275 | * @param string $type |
||
| 276 | * @param array $options |
||
| 277 | * @param bool $modify |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | 1 | public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
| 281 | { |
||
| 282 | 1 | $offset = array_search($name, array_keys($this->fields)); |
|
| 283 | |||
| 284 | 1 | $beforeFields = array_slice($this->fields, 0, $offset + 1); |
|
| 285 | 1 | $afterFields = array_slice($this->fields, $offset + 1); |
|
| 286 | |||
| 287 | 1 | $this->fields = $beforeFields; |
|
| 288 | |||
| 289 | 1 | $this->add($fieldName, $type, $options, $modify); |
|
| 290 | |||
| 291 | 1 | $this->fields += $afterFields; |
|
| 292 | |||
| 293 | 1 | return $this; |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Take another form and add it's fields directly to this form. |
||
| 298 | * |
||
| 299 | * @param mixed $class Form to merge. |
||
| 300 | * @param array $options |
||
| 301 | * @param boolean $modify |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | 1 | public function compose($class, array $options = [], $modify = false) |
|
| 305 | { |
||
| 306 | 1 | $options['class'] = $class; |
|
| 307 | |||
| 308 | // If we pass a ready made form just extract the fields. |
||
| 309 | 1 | if ($class instanceof Form) { |
|
| 310 | 1 | $fields = $class->getFields(); |
|
| 311 | } elseif ($class instanceof Fields\ChildFormType) { |
||
| 312 | $fields = $class->getForm()->getFields(); |
||
| 313 | } elseif (is_string($class)) { |
||
| 314 | // If its a string of a class make it the usual way. |
||
| 315 | $options['model'] = $this->model; |
||
| 316 | $options['name'] = $this->name; |
||
| 317 | |||
| 318 | $form = $this->formBuilder->create($class, $options); |
||
| 319 | $fields = $form->getFields(); |
||
| 320 | } else { |
||
| 321 | throw new \InvalidArgumentException( |
||
| 322 | "[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType" |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | 1 | foreach ($fields as $field) { |
|
| 327 | 1 | $this->addField($field, $modify); |
|
| 328 | } |
||
| 329 | |||
| 330 | 1 | return $this; |
|
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Remove field with specified name from the form. |
||
| 335 | * |
||
| 336 | * @param $name |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | 2 | public function remove($name) |
|
| 340 | { |
||
| 341 | 2 | if ($this->has($name)) { |
|
| 342 | 2 | unset($this->fields[$name]); |
|
| 343 | } |
||
| 344 | |||
| 345 | 2 | return $this; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Modify existing field. If it doesn't exist, it is added to form. |
||
| 350 | * |
||
| 351 | * @param string $name |
||
| 352 | * @param string $type |
||
| 353 | * @param array $options |
||
| 354 | * @param bool $overwriteOptions |
||
| 355 | * @return Form |
||
| 356 | */ |
||
| 357 | 1 | public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
|
| 358 | { |
||
| 359 | // If we don't want to overwrite options, we merge them with old options. |
||
| 360 | 1 | if ($overwriteOptions === false && $this->has($name)) { |
|
| 361 | 1 | $options = $this->formHelper->mergeOptions( |
|
| 362 | 1 | $this->getField($name)->getOptions(), |
|
| 363 | 1 | $options |
|
| 364 | ); |
||
| 365 | } |
||
| 366 | |||
| 367 | 1 | return $this->add($name, $type, $options, true); |
|
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Render full form. |
||
| 372 | * |
||
| 373 | * @param array $options |
||
| 374 | * @param bool $showStart |
||
| 375 | * @param bool $showFields |
||
| 376 | * @param bool $showEnd |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | 7 | public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
|
| 380 | { |
||
| 381 | 7 | return $this->render($options, $this->fields, $showStart, $showFields, $showEnd); |
|
|
|
|||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Render rest of the form. |
||
| 386 | * |
||
| 387 | * @param bool $showFormEnd |
||
| 388 | * @param bool $showFields |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 1 | public function renderRest($showFormEnd = true, $showFields = true) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Renders the rest of the form up until the specified field name. |
||
| 400 | * |
||
| 401 | * @param string $field_name |
||
| 402 | * @param bool $showFormEnd |
||
| 403 | * @param bool $showFields |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 2 | public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get single field instance from form object. |
||
| 429 | * |
||
| 430 | * @param string $name |
||
| 431 | * @return FormField |
||
| 432 | */ |
||
| 433 | 34 | public function getField($name) |
|
| 434 | { |
||
| 435 | 34 | if ($this->has($name)) { |
|
| 436 | 33 | return $this->fields[$name]; |
|
| 437 | } |
||
| 438 | |||
| 439 | 1 | $this->fieldDoesNotExist($name); |
|
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Check if form has field. |
||
| 444 | * |
||
| 445 | * @param string $name |
||
| 446 | * @return bool |
||
| 447 | */ |
||
| 448 | 61 | public function has($name) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Get all form options. |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | 2 | public function getFormOptions() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get single form option. |
||
| 465 | * |
||
| 466 | * @param string $option |
||
| 467 | * @param mixed|null $default |
||
| 468 | * @return mixed |
||
| 469 | */ |
||
| 470 | 129 | public function getFormOption($option, $default = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Set single form option on form. |
||
| 477 | * |
||
| 478 | * @param string $option |
||
| 479 | * @param mixed $value |
||
| 480 | * |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | 2 | public function setFormOption($option, $value) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Get the passed config key using the custom |
||
| 492 | * form config, if any. |
||
| 493 | * |
||
| 494 | * @param string $key |
||
| 495 | * @param mixed $default |
||
| 496 | * |
||
| 497 | * @return mixed |
||
| 498 | */ |
||
| 499 | 103 | public function getConfig($key = null, $default = null) |
|
| 500 | { |
||
| 501 | 103 | return $this->formHelper->getConfig($key, $default, $this->formConfig); |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Set form options. |
||
| 506 | * |
||
| 507 | * @param array $formOptions |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | 129 | public function setFormOptions(array $formOptions) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Get an option from provided options and call method with that value. |
||
| 527 | * |
||
| 528 | * @param string $name |
||
| 529 | * @param string $method |
||
| 530 | */ |
||
| 531 | 129 | protected function pullFromOptions($name, $method) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Get form http method. |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | 3 | public function getMethod() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Set form http method. |
||
| 550 | * |
||
| 551 | * @param string $method |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | 1 | public function setMethod($method) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Get form action url. |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | 3 | public function getUrl() |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Set form action url. |
||
| 573 | * |
||
| 574 | * @param string $url |
||
| 575 | * @return $this |
||
| 576 | */ |
||
| 577 | 1 | public function setUrl($url) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Returns the name of the form. |
||
| 586 | * |
||
| 587 | * @return string|null |
||
| 588 | */ |
||
| 589 | 66 | public function getName() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Set the name of the form. |
||
| 596 | * |
||
| 597 | * @param string $name |
||
| 598 | * @param bool $rebuild |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | 12 | public function setName($name, $rebuild = true) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Get model that is bind to form object. |
||
| 614 | * |
||
| 615 | * @return mixed |
||
| 616 | */ |
||
| 617 | 96 | public function getModel() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Set model to form object. |
||
| 624 | * |
||
| 625 | * @param mixed $model |
||
| 626 | * @return $this |
||
| 627 | * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
||
| 628 | */ |
||
| 629 | 17 | public function setModel($model) |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Setup model for form, add namespace if needed for child forms. |
||
| 640 | * |
||
| 641 | * @return $this |
||
| 642 | */ |
||
| 643 | 20 | protected function setupModel($model) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get all fields. |
||
| 652 | * |
||
| 653 | * @return FormField[] |
||
| 654 | */ |
||
| 655 | 129 | public function getFields() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Get field dynamically. |
||
| 662 | * |
||
| 663 | * @param string $name |
||
| 664 | * @return FormField |
||
| 665 | */ |
||
| 666 | 20 | public function __get($name) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Check if field exists when fetched using magic methods. |
||
| 675 | * |
||
| 676 | * @param string $name |
||
| 677 | * @return bool |
||
| 678 | */ |
||
| 679 | public function __isset($name) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Set the Event Dispatcher to fire Laravel events. |
||
| 686 | * |
||
| 687 | * @param EventDispatcher $eventDispatcher |
||
| 688 | * @return $this |
||
| 689 | */ |
||
| 690 | 129 | public function setEventDispatcher(EventDispatcher $eventDispatcher) |
|
| 696 | |||
| 697 | /** |
||
| 698 | * Set the form helper only on first instantiation. |
||
| 699 | * |
||
| 700 | * @param FormHelper $formHelper |
||
| 701 | * @return $this |
||
| 702 | */ |
||
| 703 | 129 | public function setFormHelper(FormHelper $formHelper) |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Get form helper. |
||
| 712 | * |
||
| 713 | * @return FormHelper |
||
| 714 | */ |
||
| 715 | 101 | public function getFormHelper() |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Add custom field. |
||
| 722 | * |
||
| 723 | * @param $name |
||
| 724 | * @param $class |
||
| 725 | */ |
||
| 726 | 2 | public function addCustomField($name, $class) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Returns wether form errors should be shown under every field. |
||
| 737 | * |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | 101 | public function haveErrorsEnabled() |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Enable or disable showing errors under fields |
||
| 747 | * |
||
| 748 | * @param bool $enabled |
||
| 749 | * @return $this |
||
| 750 | */ |
||
| 751 | 1 | public function setErrorsEnabled($enabled) |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Is client validation enabled? |
||
| 760 | * |
||
| 761 | * @return bool |
||
| 762 | */ |
||
| 763 | 101 | public function clientValidationEnabled() |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Enable/disable client validation. |
||
| 770 | * |
||
| 771 | * @param bool $enable |
||
| 772 | * @return $this |
||
| 773 | */ |
||
| 774 | 2 | public function setClientValidationEnabled($enable) |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Add any aditional data that field needs (ex. array of choices). |
||
| 783 | * |
||
| 784 | * @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 |
||
| 785 | * will be switched to protected in 1.7. |
||
| 786 | * @param string $name |
||
| 787 | * @param mixed $data |
||
| 788 | */ |
||
| 789 | 1 | public function setData($name, $data) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Get single additional data. |
||
| 796 | * |
||
| 797 | * @param string $name |
||
| 798 | * @param null $default |
||
| 799 | * @return mixed |
||
| 800 | */ |
||
| 801 | 20 | public function getData($name = null, $default = null) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Add multiple peices of data at once. |
||
| 812 | * |
||
| 813 | * @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 |
||
| 814 | * will be switched to protected in 1.7. |
||
| 815 | * @param $data |
||
| 816 | * @return $this |
||
| 817 | **/ |
||
| 818 | 129 | public function addData(array $data) |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Get current request. |
||
| 829 | * |
||
| 830 | * @return \Illuminate\Http\Request |
||
| 831 | */ |
||
| 832 | 129 | public function getRequest() |
|
| 836 | |||
| 837 | /** |
||
| 838 | * Set request on form. |
||
| 839 | * |
||
| 840 | * @param Request $request |
||
| 841 | * @return $this |
||
| 842 | */ |
||
| 843 | 129 | public function setRequest(Request $request) |
|
| 849 | |||
| 850 | /** |
||
| 851 | * Get template prefix that is prepended to all template paths. |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | 39 | public function getTemplatePrefix() |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Set a template prefix for the form and its fields. |
||
| 866 | * |
||
| 867 | * @param string $prefix |
||
| 868 | * @return $this |
||
| 869 | */ |
||
| 870 | 4 | public function setTemplatePrefix($prefix) |
|
| 876 | |||
| 877 | /** |
||
| 878 | * Get the language name. |
||
| 879 | * |
||
| 880 | * @return string |
||
| 881 | */ |
||
| 882 | 98 | public function getLanguageName() |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Set a language name, used as prefix for translated strings. |
||
| 889 | * |
||
| 890 | * @param string $prefix |
||
| 891 | * @return $this |
||
| 892 | */ |
||
| 893 | 13 | public function setLanguageName($prefix) |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Get the translation template. |
||
| 902 | * |
||
| 903 | * @return string |
||
| 904 | */ |
||
| 905 | 100 | public function getTranslationTemplate() |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Set a translation template, used to determine labels for fields. |
||
| 912 | * |
||
| 913 | * @param string $template |
||
| 914 | * @return $this |
||
| 915 | */ |
||
| 916 | 11 | public function setTranslationTemplate($template) |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Render the form. |
||
| 925 | * |
||
| 926 | * @param array $options |
||
| 927 | * @param string $fields |
||
| 928 | * @param bool $showStart |
||
| 929 | * @param bool $showFields |
||
| 930 | * @param bool $showEnd |
||
| 931 | * @return string |
||
| 932 | */ |
||
| 933 | 9 | protected function render($options, $fields, $showStart, $showFields, $showEnd) |
|
| 949 | |||
| 950 | /** |
||
| 951 | * Get template from options if provided, otherwise fallback to config. |
||
| 952 | * |
||
| 953 | * @return mixed |
||
| 954 | */ |
||
| 955 | 9 | protected function getTemplate() |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Get all fields that are not rendered. |
||
| 962 | * |
||
| 963 | * @return array |
||
| 964 | */ |
||
| 965 | 2 | protected function getUnrenderedFields() |
|
| 978 | |||
| 979 | /** |
||
| 980 | * Prevent adding fields with same name. |
||
| 981 | * |
||
| 982 | * @param string $name |
||
| 983 | * @throws \InvalidArgumentException |
||
| 984 | * @return void |
||
| 985 | */ |
||
| 986 | 61 | protected function preventDuplicate($name) |
|
| 992 | |||
| 993 | /** |
||
| 994 | * Returns and checks the type of the field. |
||
| 995 | * |
||
| 996 | * @param string $type |
||
| 997 | * @return string |
||
| 998 | */ |
||
| 999 | 65 | protected function getFieldType($type) |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Check if form is named form. |
||
| 1008 | * |
||
| 1009 | * @return void |
||
| 1010 | */ |
||
| 1011 | 129 | protected function checkIfNamedForm() |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Set up options on single field depending on form options. |
||
| 1020 | * |
||
| 1021 | * @param string $name |
||
| 1022 | * @param $options |
||
| 1023 | */ |
||
| 1024 | 65 | protected function setupFieldOptions($name, &$options) |
|
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Set namespace to model if form is named so the data is bound properly. |
||
| 1031 | * Returns true if model is changed, otherwise false. |
||
| 1032 | * |
||
| 1033 | * @return bool |
||
| 1034 | */ |
||
| 1035 | 9 | protected function setupNamedModel() |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Set form builder instance on helper so we can use it later. |
||
| 1057 | * |
||
| 1058 | * @param FormBuilder $formBuilder |
||
| 1059 | * @return $this |
||
| 1060 | */ |
||
| 1061 | 129 | public function setFormBuilder(FormBuilder $formBuilder) |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Returns the instance of the FormBuilder. |
||
| 1070 | * |
||
| 1071 | * @return FormBuilder |
||
| 1072 | */ |
||
| 1073 | 12 | public function getFormBuilder() |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Set the Validator instance on this so we can use it later. |
||
| 1080 | * |
||
| 1081 | * @param ValidatorFactory $validator |
||
| 1082 | * @return $this |
||
| 1083 | */ |
||
| 1084 | 129 | public function setValidator(ValidatorFactory $validator) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Returns the validator instance. |
||
| 1093 | * |
||
| 1094 | * @return Validator |
||
| 1095 | */ |
||
| 1096 | 1 | public function getValidator() |
|
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Exclude some fields from rendering. |
||
| 1103 | * |
||
| 1104 | * @return $this |
||
| 1105 | */ |
||
| 1106 | public function exclude(array $fields) |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * If form is named form, modify names to be contained in single key (parent[child_field_name]). |
||
| 1115 | * |
||
| 1116 | * @param string $name |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | 65 | protected function getFieldName($name) |
|
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Disable all fields in a form. |
||
| 1139 | */ |
||
| 1140 | 1 | public function disableFields() |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Enable all fields in a form. |
||
| 1149 | */ |
||
| 1150 | 1 | public function enableFields() |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Validate the form. |
||
| 1159 | * |
||
| 1160 | * @param array $validationRules |
||
| 1161 | * @param array $messages |
||
| 1162 | * @return Validator |
||
| 1163 | */ |
||
| 1164 | 9 | public function validate($validationRules = [], $messages = []) |
|
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Get validation rules for the form. |
||
| 1182 | * |
||
| 1183 | * @param array $overrideRules |
||
| 1184 | * @return array |
||
| 1185 | */ |
||
| 1186 | 1 | public function getRules($overrideRules = []) |
|
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Redirects to a destination when form is invalid. |
||
| 1195 | * |
||
| 1196 | * @param string|null $destination The target url. |
||
| 1197 | * @return HttpResponseException |
||
| 1198 | */ |
||
| 1199 | 3 | public function redirectIfNotValid($destination = null) |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Get all form field attributes, including child forms, in a flat array. |
||
| 1216 | * |
||
| 1217 | * @return array |
||
| 1218 | */ |
||
| 1219 | 3 | public function getAllAttributes() |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Check if the form is valid. |
||
| 1226 | * |
||
| 1227 | * @return bool |
||
| 1228 | */ |
||
| 1229 | 9 | public function isValid() |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Optionally change the validation result, and/or add error messages. |
||
| 1246 | * |
||
| 1247 | * @param Form $mainForm |
||
| 1248 | * @param bool $isValid |
||
| 1249 | * @return void|array |
||
| 1250 | */ |
||
| 1251 | 9 | public function alterValid(Form $mainForm, &$isValid) |
|
| 1255 | |||
| 1256 | /** |
||
| 1257 | * Get validation errors. |
||
| 1258 | * |
||
| 1259 | * @return array |
||
| 1260 | */ |
||
| 1261 | 8 | public function getErrors() |
|
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Get all Request values from all fields, and nothing else. |
||
| 1277 | * |
||
| 1278 | * @param bool $with_nulls |
||
| 1279 | * @return array |
||
| 1280 | */ |
||
| 1281 | 3 | public function getFieldValues($with_nulls = true) |
|
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Optionally mess with this form's $values before it's returned from getFieldValues(). |
||
| 1307 | * |
||
| 1308 | * @param array $values |
||
| 1309 | * @return void |
||
| 1310 | */ |
||
| 1311 | 3 | public function alterFieldValues(array &$values) |
|
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Throw an exception indicating a field does not exist on the class. |
||
| 1317 | * |
||
| 1318 | * @param string $name |
||
| 1319 | * @throws \InvalidArgumentException |
||
| 1320 | * @return void |
||
| 1321 | */ |
||
| 1322 | 2 | protected function fieldDoesNotExist($name) |
|
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Method filterFields used as *Main* method for starting |
||
| 1329 | * filtering and request field mutating process. |
||
| 1330 | * |
||
| 1331 | * @return \Kris\LaravelFormBuilder\Form |
||
| 1332 | */ |
||
| 1333 | 129 | public function filterFields() |
|
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Method getFilters used to return array of all binded filters to form fields. |
||
| 1362 | * |
||
| 1363 | * @return array |
||
| 1364 | */ |
||
| 1365 | 129 | public function getFilters() |
|
| 1374 | |||
| 1375 | /** |
||
| 1376 | * If lockFiltering is set to true then we will not |
||
| 1377 | * filter fields and mutate request data binded to fields. |
||
| 1378 | * |
||
| 1379 | * @return \Kris\LaravelFormBuilder\Form |
||
| 1380 | */ |
||
| 1381 | 1 | public function lockFiltering() |
|
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Unlock fields filtering/mutating. |
||
| 1389 | * |
||
| 1390 | * @return \Kris\LaravelFormBuilder\Form |
||
| 1391 | */ |
||
| 1392 | public function unlockFiltering() |
||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Method isFilteringLocked used to check |
||
| 1400 | * if current filteringLocked property status is set to true. |
||
| 1401 | * |
||
| 1402 | * @return bool |
||
| 1403 | */ |
||
| 1404 | 129 | public function isFilteringLocked() |
|
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Method getRawValues returns Unfiltered/Unmutated fields -> values. |
||
| 1411 | * |
||
| 1412 | * @return array |
||
| 1413 | */ |
||
| 1414 | public function getRawValues() |
||
| 1423 | } |
||
| 1424 |
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: