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 |
||
| 67 | class Form extends ViewableData implements HasRequestHandler |
||
| 68 | { |
||
| 69 | use FormMessage; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Default form Name property |
||
| 73 | */ |
||
| 74 | const DEFAULT_NAME = 'Form'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Form submission data is URL encoded |
||
| 78 | */ |
||
| 79 | const ENC_TYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Form submission data is multipart form |
||
| 83 | */ |
||
| 84 | const ENC_TYPE_MULTIPART = 'multipart/form-data'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Accessed by Form.ss; modified by {@link formHtmlContent()}. |
||
| 88 | * A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously |
||
| 89 | * |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | public $IncludeFormTag = true; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var FieldList |
||
| 96 | */ |
||
| 97 | protected $fields; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var FieldList |
||
| 101 | */ |
||
| 102 | protected $actions; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Parent (optional) request handler |
||
| 106 | * |
||
| 107 | * @var RequestHandler |
||
| 108 | */ |
||
| 109 | protected $controller; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $name; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var Validator |
||
| 118 | */ |
||
| 119 | protected $validator; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @see setValidationResponseCallback() |
||
| 123 | * @var callable |
||
| 124 | */ |
||
| 125 | protected $validationResponseCallback; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $formMethod = "POST"; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var boolean |
||
| 134 | */ |
||
| 135 | protected $strictFormMethodCheck = true; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Populated by {@link loadDataFrom()}. |
||
| 139 | * |
||
| 140 | * @var DataObject|null |
||
| 141 | */ |
||
| 142 | protected $record; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Keeps track of whether this form has a default action or not. |
||
| 146 | * Set to false by $this->disableDefaultAction(); |
||
| 147 | * |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | protected $hasDefaultAction = true; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Target attribute of form-tag. |
||
| 154 | * Useful to open a new window upon |
||
| 155 | * form submission. |
||
| 156 | * |
||
| 157 | * @var string|null |
||
| 158 | */ |
||
| 159 | protected $target; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Legend value, to be inserted into the |
||
| 163 | * <legend> element before the <fieldset> |
||
| 164 | * in Form.ss template. |
||
| 165 | * |
||
| 166 | * @var string|null |
||
| 167 | */ |
||
| 168 | protected $legend; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The SS template to render this form HTML into. |
||
| 172 | * Default is "Form", but this can be changed to |
||
| 173 | * another template for customisation. |
||
| 174 | * |
||
| 175 | * @see Form->setTemplate() |
||
| 176 | * @var string|null |
||
| 177 | */ |
||
| 178 | protected $template; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Should we redirect the user back down to the |
||
| 182 | * the form on validation errors rather then just the page |
||
| 183 | * |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | protected $redirectToFormOnValidationError = false; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var bool |
||
| 190 | */ |
||
| 191 | protected $security = true; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var SecurityToken|null |
||
| 195 | */ |
||
| 196 | protected $securityToken = null; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * List of additional CSS classes for the form tag. |
||
| 200 | * |
||
| 201 | * @var array |
||
| 202 | */ |
||
| 203 | protected $extraClasses = array(); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @config |
||
| 207 | * @var array $default_classes The default classes to apply to the Form |
||
| 208 | */ |
||
| 209 | private static $default_classes = array(); |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @var string|null |
||
| 213 | */ |
||
| 214 | protected $encType; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Any custom form attributes set through {@link setAttributes()}. |
||
| 218 | * Some attributes are calculated on the fly, so please use {@link getAttributes()} to access them. |
||
| 219 | * |
||
| 220 | * @var array |
||
| 221 | */ |
||
| 222 | protected $attributes = array(); |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var array |
||
| 226 | */ |
||
| 227 | protected $validationExemptActions = array(); |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @config |
||
| 231 | * @var array |
||
| 232 | */ |
||
| 233 | private static $casting = array( |
||
| 234 | 'AttributesHTML' => 'HTMLFragment', |
||
| 235 | 'FormAttributes' => 'HTMLFragment', |
||
| 236 | 'FormName' => 'Text', |
||
| 237 | 'Legend' => 'HTMLFragment', |
||
| 238 | ); |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @var FormTemplateHelper |
||
| 242 | */ |
||
| 243 | private $templateHelper = null; |
||
| 244 | |||
| 245 | /** |
||
| 246 | * HTML ID for this form. |
||
| 247 | * |
||
| 248 | * @var string |
||
| 249 | */ |
||
| 250 | private $htmlID = null; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Custom form action path, if not linking to itself. |
||
| 254 | * E.g. could be used to post to an external link |
||
| 255 | * |
||
| 256 | * @var string |
||
| 257 | */ |
||
| 258 | protected $formActionPath = false; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @var bool |
||
| 262 | */ |
||
| 263 | protected $securityTokenAdded = false; |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @var bool |
||
| 267 | */ |
||
| 268 | protected $notifyUnsavedChanges = false; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Create a new form, with the given fields an action buttons. |
||
| 272 | * |
||
| 273 | * @param RequestHandler $controller Optional parent request handler |
||
| 274 | * @param string $name The method on the controller that will return this form object. |
||
| 275 | * @param FieldList $fields All of the fields in the form - a {@link FieldList} of {@link FormField} objects. |
||
| 276 | * @param FieldList $actions All of the action buttons in the form - a {@link FieldLis} of |
||
| 277 | * {@link FormAction} objects |
||
| 278 | * @param Validator|null $validator Override the default validator instance (Default: {@link RequiredFields}) |
||
| 279 | */ |
||
| 280 | public function __construct( |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function getNotifyUnsavedChanges() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param bool |
||
| 327 | */ |
||
| 328 | public function setNotifyUnsavedChanges($flag) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Load form state from session state |
||
| 335 | * |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function restoreFormState() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Flush persistant form state details |
||
| 356 | * |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | public function clearFormState() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Helper to get current request for this form |
||
| 370 | * |
||
| 371 | * @return HTTPRequest |
||
| 372 | */ |
||
| 373 | protected function getRequest() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get session for this form |
||
| 389 | * |
||
| 390 | * @return Session |
||
| 391 | */ |
||
| 392 | protected function getSession() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return any form data stored in the session |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function getSessionData() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Store the given form data in the session |
||
| 413 | * |
||
| 414 | * @param array $data |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function setSessionData($data) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return any ValidationResult instance stored for this object |
||
| 425 | * |
||
| 426 | * @return ValidationResult The ValidationResult object stored in the session |
||
| 427 | */ |
||
| 428 | public function getSessionValidationResult() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Sets the ValidationResult in the session to be used with the next view of this form. |
||
| 439 | * @param ValidationResult $result The result to save |
||
| 440 | * @param bool $combineWithExisting If true, then this will be added to the existing result. |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function setSessionValidationResult(ValidationResult $result, $combineWithExisting = false) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Clear form message (and in session) |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | public function clearMessage() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Populate this form with messages from the given ValidationResult. |
||
| 477 | * Note: This will not clear any pre-existing messages |
||
| 478 | * |
||
| 479 | * @param ValidationResult $result |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function loadMessagesFrom($result) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set message on a given field name. This message will not persist via redirect. |
||
| 499 | * |
||
| 500 | * @param string $fieldName |
||
| 501 | * @param string $message |
||
| 502 | * @param string $messageType |
||
| 503 | * @param string $messageCast |
||
| 504 | * @return $this |
||
| 505 | */ |
||
| 506 | public function setFieldMessage( |
||
| 518 | |||
| 519 | public function castingHelper($field) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * set up the default classes for the form. This is done on construct so that the default classes can be removed |
||
| 530 | * after instantiation |
||
| 531 | */ |
||
| 532 | protected function setupDefaultClasses() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @return callable |
||
| 544 | */ |
||
| 545 | public function getValidationResponseCallback() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Overrules validation error behaviour in {@link httpSubmission()} |
||
| 552 | * when validation has failed. Useful for optional handling of a certain accepted content type. |
||
| 553 | * |
||
| 554 | * The callback can opt out of handling specific responses by returning NULL, |
||
| 555 | * in which case the default form behaviour will kick in. |
||
| 556 | * |
||
| 557 | * @param $callback |
||
| 558 | * @return self |
||
| 559 | */ |
||
| 560 | public function setValidationResponseCallback($callback) |
||
| 566 | /** |
||
| 567 | * Convert this form into a readonly form |
||
| 568 | */ |
||
| 569 | public function makeReadonly() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set whether the user should be redirected back down to the |
||
| 576 | * form on the page upon validation errors in the form or if |
||
| 577 | * they just need to redirect back to the page |
||
| 578 | * |
||
| 579 | * @param bool $bool Redirect to form on error? |
||
| 580 | * @return $this |
||
| 581 | */ |
||
| 582 | public function setRedirectToFormOnValidationError($bool) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Get whether the user should be redirected back down to the |
||
| 590 | * form on the page upon validation errors |
||
| 591 | * |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | public function getRedirectToFormOnValidationError() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param FormTransformation $trans |
||
| 601 | */ |
||
| 602 | public function transform(FormTransformation $trans) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the {@link Validator} attached to this form. |
||
| 625 | * @return Validator |
||
| 626 | */ |
||
| 627 | public function getValidator() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Set the {@link Validator} on this form. |
||
| 634 | * @param Validator $validator |
||
| 635 | * @return $this |
||
| 636 | */ |
||
| 637 | public function setValidator(Validator $validator) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Remove the {@link Validator} from this from. |
||
| 648 | */ |
||
| 649 | public function unsetValidator() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Set actions that are exempt from validation |
||
| 657 | * |
||
| 658 | * @param array |
||
| 659 | * @return $this |
||
| 660 | */ |
||
| 661 | public function setValidationExemptActions($actions) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get a list of actions that are exempt from validation |
||
| 669 | * |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | public function getValidationExemptActions() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Passed a FormAction, returns true if that action is exempt from Form validation |
||
| 679 | * |
||
| 680 | * @param FormAction $action |
||
| 681 | * @return bool |
||
| 682 | */ |
||
| 683 | public function actionIsValidationExempt($action) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Generate extra special fields - namely the security token field (if required). |
||
| 700 | * |
||
| 701 | * @return FieldList |
||
| 702 | */ |
||
| 703 | public function getExtraFields() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Return the form's fields - used by the templates |
||
| 728 | * |
||
| 729 | * @return FieldList The form fields |
||
| 730 | */ |
||
| 731 | public function Fields() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Return all <input type="hidden"> fields |
||
| 744 | * in a form - including fields nested in {@link CompositeFields}. |
||
| 745 | * Useful when doing custom field layouts. |
||
| 746 | * |
||
| 747 | * @return FieldList |
||
| 748 | */ |
||
| 749 | public function HiddenFields() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Return all fields except for the hidden fields. |
||
| 756 | * Useful when making your own simplified form layouts. |
||
| 757 | */ |
||
| 758 | public function VisibleFields() |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Setter for the form fields. |
||
| 765 | * |
||
| 766 | * @param FieldList $fields |
||
| 767 | * @return $this |
||
| 768 | */ |
||
| 769 | public function setFields($fields) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Return the form's action buttons - used by the templates |
||
| 777 | * |
||
| 778 | * @return FieldList The action list |
||
| 779 | */ |
||
| 780 | public function Actions() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Setter for the form actions. |
||
| 787 | * |
||
| 788 | * @param FieldList $actions |
||
| 789 | * @return $this |
||
| 790 | */ |
||
| 791 | public function setActions($actions) |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Unset all form actions |
||
| 799 | */ |
||
| 800 | public function unsetAllActions() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @param string $name |
||
| 808 | * @param string $value |
||
| 809 | * @return $this |
||
| 810 | */ |
||
| 811 | public function setAttribute($name, $value) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @param string $name |
||
| 819 | * @return string |
||
| 820 | */ |
||
| 821 | public function getAttribute($name) |
||
| 828 | |||
| 829 | /** |
||
| 830 | * @return array |
||
| 831 | */ |
||
| 832 | public function getAttributes() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Return the attributes of the form tag - used by the templates. |
||
| 857 | * |
||
| 858 | * @param array $attrs Custom attributes to process. Falls back to {@link getAttributes()}. |
||
| 859 | * If at least one argument is passed as a string, all arguments act as excludes by name. |
||
| 860 | * |
||
| 861 | * @return string HTML attributes, ready for insertion into an HTML tag |
||
| 862 | */ |
||
| 863 | public function getAttributesHTML($attrs = null) |
||
| 911 | |||
| 912 | public function FormAttributes() |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Set the target of this form to any value - useful for opening the form contents in a new window or refreshing |
||
| 919 | * another frame |
||
| 920 | * |
||
| 921 | * @param string|FormTemplateHelper |
||
| 922 | */ |
||
| 923 | public function setTemplateHelper($helper) |
||
| 927 | |||
| 928 | /** |
||
| 929 | * Return a {@link FormTemplateHelper} for this form. If one has not been |
||
| 930 | * set, return the default helper. |
||
| 931 | * |
||
| 932 | * @return FormTemplateHelper |
||
| 933 | */ |
||
| 934 | public function getTemplateHelper() |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Set the target of this form to any value - useful for opening the form |
||
| 949 | * contents in a new window or refreshing another frame. |
||
| 950 | * |
||
| 951 | * @param string $target The value of the target |
||
| 952 | * @return $this |
||
| 953 | */ |
||
| 954 | public function setTarget($target) |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Set the legend value to be inserted into |
||
| 963 | * the <legend> element in the Form.ss template. |
||
| 964 | * @param string $legend |
||
| 965 | * @return $this |
||
| 966 | */ |
||
| 967 | public function setLegend($legend) |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Set the SS template that this form should use |
||
| 975 | * to render with. The default is "Form". |
||
| 976 | * |
||
| 977 | * @param string $template The name of the template (without the .ss extension) |
||
| 978 | * @return $this |
||
| 979 | */ |
||
| 980 | public function setTemplate($template) |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Return the template to render this form with. |
||
| 988 | * |
||
| 989 | * @return string |
||
| 990 | */ |
||
| 991 | public function getTemplate() |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Returs the ordered list of preferred templates for rendering this form |
||
| 998 | * If the template isn't set, then default to the |
||
| 999 | * form class name e.g "Form". |
||
| 1000 | * |
||
| 1001 | * @return array |
||
| 1002 | */ |
||
| 1003 | public function getTemplates() |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Returns the encoding type for the form. |
||
| 1015 | * |
||
| 1016 | * By default this will be URL encoded, unless there is a file field present |
||
| 1017 | * in which case multipart is used. You can also set the enc type using |
||
| 1018 | * {@link setEncType}. |
||
| 1019 | */ |
||
| 1020 | public function getEncType() |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Sets the form encoding type. The most common encoding types are defined |
||
| 1039 | * in {@link ENC_TYPE_URLENCODED} and {@link ENC_TYPE_MULTIPART}. |
||
| 1040 | * |
||
| 1041 | * @param string $encType |
||
| 1042 | * @return $this |
||
| 1043 | */ |
||
| 1044 | public function setEncType($encType) |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Returns the real HTTP method for the form: |
||
| 1052 | * GET, POST, PUT, DELETE or HEAD. |
||
| 1053 | * As most browsers only support GET and POST in |
||
| 1054 | * form submissions, all other HTTP methods are |
||
| 1055 | * added as a hidden field "_method" that |
||
| 1056 | * gets evaluated in {@link HTTPRequest::detect_method()}. |
||
| 1057 | * See {@link FormMethod()} to get a HTTP method |
||
| 1058 | * for safe insertion into a <form> tag. |
||
| 1059 | * |
||
| 1060 | * @return string HTTP method |
||
| 1061 | */ |
||
| 1062 | public function FormHttpMethod() |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Returns the form method to be used in the <form> tag. |
||
| 1069 | * See {@link FormHttpMethod()} to get the "real" method. |
||
| 1070 | * |
||
| 1071 | * @return string Form HTTP method restricted to 'GET' or 'POST' |
||
| 1072 | */ |
||
| 1073 | public function FormMethod() |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Set the form method: GET, POST, PUT, DELETE. |
||
| 1084 | * |
||
| 1085 | * @param string $method |
||
| 1086 | * @param bool $strict If non-null, pass value to {@link setStrictFormMethodCheck()}. |
||
| 1087 | * @return $this |
||
| 1088 | */ |
||
| 1089 | public function setFormMethod($method, $strict = null) |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * If set to true (the default), enforces the matching of the form method. |
||
| 1100 | * |
||
| 1101 | * This will mean two things: |
||
| 1102 | * - GET vars will be ignored by a POST form, and vice versa |
||
| 1103 | * - A submission where the HTTP method used doesn't match the form will return a 400 error. |
||
| 1104 | * |
||
| 1105 | * If set to false then the form method is only used to construct the default |
||
| 1106 | * form. |
||
| 1107 | * |
||
| 1108 | * @param $bool boolean |
||
| 1109 | * @return $this |
||
| 1110 | */ |
||
| 1111 | public function setStrictFormMethodCheck($bool) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @return boolean |
||
| 1119 | */ |
||
| 1120 | public function getStrictFormMethodCheck() |
||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Return the form's action attribute. |
||
| 1127 | * This is build by adding an executeForm get variable to the parent controller's Link() value |
||
| 1128 | * |
||
| 1129 | * @return string |
||
| 1130 | */ |
||
| 1131 | public function FormAction() |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Set the form action attribute to a custom URL. |
||
| 1143 | * |
||
| 1144 | * Note: For "normal" forms, you shouldn't need to use this method. It is |
||
| 1145 | * recommended only for situations where you have two relatively distinct |
||
| 1146 | * parts of the system trying to communicate via a form post. |
||
| 1147 | * |
||
| 1148 | * @param string $path |
||
| 1149 | * @return $this |
||
| 1150 | */ |
||
| 1151 | public function setFormAction($path) |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Returns the name of the form. |
||
| 1160 | * |
||
| 1161 | * @return string |
||
| 1162 | */ |
||
| 1163 | public function FormName() |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Set the HTML ID attribute of the form. |
||
| 1170 | * |
||
| 1171 | * @param string $id |
||
| 1172 | * @return $this |
||
| 1173 | */ |
||
| 1174 | public function setHTMLID($id) |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * @return string |
||
| 1183 | */ |
||
| 1184 | public function getHTMLID() |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Get the controller or parent request handler. |
||
| 1191 | * |
||
| 1192 | * @return RequestHandler |
||
| 1193 | */ |
||
| 1194 | public function getController() |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Set the controller or parent request handler. |
||
| 1201 | * |
||
| 1202 | * @param RequestHandler $controller |
||
| 1203 | * @return $this |
||
| 1204 | */ |
||
| 1205 | public function setController(RequestHandler $controller = null) |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Get the name of the form. |
||
| 1213 | * |
||
| 1214 | * @return string |
||
| 1215 | */ |
||
| 1216 | public function getName() |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Set the name of the form. |
||
| 1223 | * |
||
| 1224 | * @param string $name |
||
| 1225 | * @return Form |
||
| 1226 | */ |
||
| 1227 | public function setName($name) |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Returns an object where there is a method with the same name as each data |
||
| 1236 | * field on the form. |
||
| 1237 | * |
||
| 1238 | * That method will return the field itself. |
||
| 1239 | * |
||
| 1240 | * It means that you can execute $firstName = $form->FieldMap()->FirstName() |
||
| 1241 | */ |
||
| 1242 | public function FieldMap() |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Set a message to the session, for display next time this form is shown. |
||
| 1249 | * |
||
| 1250 | * @param string $message the text of the message |
||
| 1251 | * @param string $type Should be set to good, bad, or warning. |
||
| 1252 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
| 1253 | * Bool values will be treated as plain text flag. |
||
| 1254 | */ |
||
| 1255 | public function sessionMessage($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT) |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Set an error to the session, for display next time this form is shown. |
||
| 1265 | * |
||
| 1266 | * @param string $message the text of the message |
||
| 1267 | * @param string $type Should be set to good, bad, or warning. |
||
| 1268 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
| 1269 | * Bool values will be treated as plain text flag. |
||
| 1270 | */ |
||
| 1271 | public function sessionError($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT) |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Returns the DataObject that has given this form its data |
||
| 1281 | * through {@link loadDataFrom()}. |
||
| 1282 | * |
||
| 1283 | * @return DataObject |
||
| 1284 | */ |
||
| 1285 | public function getRecord() |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Get the legend value to be inserted into the |
||
| 1292 | * <legend> element in Form.ss |
||
| 1293 | * |
||
| 1294 | * @return string |
||
| 1295 | */ |
||
| 1296 | public function getLegend() |
||
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Processing that occurs before a form is executed. |
||
| 1303 | * |
||
| 1304 | * This includes form validation, if it fails, we throw a ValidationException |
||
| 1305 | * |
||
| 1306 | * This includes form validation, if it fails, we redirect back |
||
| 1307 | * to the form with appropriate error messages. |
||
| 1308 | * Always return true if the current form action is exempt from validation |
||
| 1309 | * |
||
| 1310 | * Triggered through {@link httpSubmission()}. |
||
| 1311 | * |
||
| 1312 | * |
||
| 1313 | * Note that CSRF protection takes place in {@link httpSubmission()}, |
||
| 1314 | * if it fails the form data will never reach this method. |
||
| 1315 | * |
||
| 1316 | * @return ValidationResult |
||
| 1317 | */ |
||
| 1318 | public function validationResult() |
||
| 1334 | |||
| 1335 | const MERGE_DEFAULT = 0; |
||
| 1336 | const MERGE_CLEAR_MISSING = 1; |
||
| 1337 | const MERGE_IGNORE_FALSEISH = 2; |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Load data from the given DataObject or array. |
||
| 1341 | * |
||
| 1342 | * It will call $object->MyField to get the value of MyField. |
||
| 1343 | * If you passed an array, it will call $object[MyField]. |
||
| 1344 | * Doesn't save into dataless FormFields ({@link DatalessField}), |
||
| 1345 | * as determined by {@link FieldList->dataFields()}. |
||
| 1346 | * |
||
| 1347 | * By default, if a field isn't set (as determined by isset()), |
||
| 1348 | * its value will not be saved to the field, retaining |
||
| 1349 | * potential existing values. |
||
| 1350 | * |
||
| 1351 | * Passed data should not be escaped, and is saved to the FormField instances unescaped. |
||
| 1352 | * Escaping happens automatically on saving the data through {@link saveInto()}. |
||
| 1353 | * |
||
| 1354 | * Escaping happens automatically on saving the data through |
||
| 1355 | * {@link saveInto()}. |
||
| 1356 | * |
||
| 1357 | * @uses FieldList->dataFields() |
||
| 1358 | * @uses FormField->setValue() |
||
| 1359 | * |
||
| 1360 | * @param array|DataObject $data |
||
| 1361 | * @param int $mergeStrategy |
||
| 1362 | * For every field, {@link $data} is interrogated whether it contains a relevant property/key, and |
||
| 1363 | * what that property/key's value is. |
||
| 1364 | * |
||
| 1365 | * By default, if {@link $data} does contain a property/key, the fields value is always replaced by {@link $data}'s |
||
| 1366 | * value, even if that value is null/false/etc. Fields which don't match any property/key in {@link $data} are |
||
| 1367 | * "left alone", meaning they retain any previous value. |
||
| 1368 | * |
||
| 1369 | * You can pass a bitmask here to change this behaviour. |
||
| 1370 | * |
||
| 1371 | * Passing CLEAR_MISSING means that any fields that don't match any property/key in |
||
| 1372 | * {@link $data} are cleared. |
||
| 1373 | * |
||
| 1374 | * Passing IGNORE_FALSEISH means that any false-ish value in {@link $data} won't replace |
||
| 1375 | * a field's value. |
||
| 1376 | * |
||
| 1377 | * For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing |
||
| 1378 | * CLEAR_MISSING |
||
| 1379 | * |
||
| 1380 | * @param array $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1381 | * form that has some fields that save to one object, and some that save to another. |
||
| 1382 | * @return $this |
||
| 1383 | */ |
||
| 1384 | public function loadDataFrom($data, $mergeStrategy = 0, $fieldList = null) |
||
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Save the contents of this form into the given data object. |
||
| 1498 | * It will make use of setCastedField() to do this. |
||
| 1499 | * |
||
| 1500 | * @param DataObjectInterface $dataObject The object to save data into |
||
| 1501 | * @param FieldList $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1502 | * form that has some fields that save to one object, and some that save to another. |
||
| 1503 | */ |
||
| 1504 | public function saveInto(DataObjectInterface $dataObject, $fieldList = null) |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Get the submitted data from this form through |
||
| 1532 | * {@link FieldList->dataFields()}, which filters out |
||
| 1533 | * any form-specific data like form-actions. |
||
| 1534 | * Calls {@link FormField->dataValue()} on each field, |
||
| 1535 | * which returns a value suitable for insertion into a DataObject |
||
| 1536 | * property. |
||
| 1537 | * |
||
| 1538 | * @return array |
||
| 1539 | */ |
||
| 1540 | public function getData() |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Return a rendered version of this form. |
||
| 1559 | * |
||
| 1560 | * This is returned when you access a form as $FormObject rather |
||
| 1561 | * than <% with FormObject %> |
||
| 1562 | * |
||
| 1563 | * @return DBHTMLText |
||
| 1564 | */ |
||
| 1565 | public function forTemplate() |
||
| 1574 | |||
| 1575 | /** |
||
| 1576 | * Return a rendered version of this form, suitable for ajax post-back. |
||
| 1577 | * |
||
| 1578 | * It triggers slightly different behaviour, such as disabling the rewriting |
||
| 1579 | * of # links. |
||
| 1580 | * |
||
| 1581 | * @return DBHTMLText |
||
| 1582 | */ |
||
| 1583 | public function forAjaxTemplate() |
||
| 1594 | |||
| 1595 | /** |
||
| 1596 | * Returns an HTML rendition of this form, without the <form> tag itself. |
||
| 1597 | * |
||
| 1598 | * Attaches 3 extra hidden files, _form_action, _form_name, _form_method, |
||
| 1599 | * and _form_enctype. These are the attributes of the form. These fields |
||
| 1600 | * can be used to send the form to Ajax. |
||
| 1601 | * |
||
| 1602 | * @deprecated 5.0 |
||
| 1603 | * @return string |
||
| 1604 | */ |
||
| 1605 | public function formHtmlContent() |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Render this form using the given template, and return the result as a string |
||
| 1623 | * You can pass either an SSViewer or a template name |
||
| 1624 | * @param string|array $template |
||
| 1625 | * @return DBHTMLText |
||
| 1626 | */ |
||
| 1627 | public function renderWithoutActionButton($template) |
||
| 1639 | |||
| 1640 | /** |
||
| 1641 | * Return the default button that should be clicked when another one isn't |
||
| 1642 | * available. |
||
| 1643 | * |
||
| 1644 | * @return FormAction |
||
| 1645 | */ |
||
| 1646 | public function defaultAction() |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Disable the default button. |
||
| 1656 | * |
||
| 1657 | * Ordinarily, when a form is processed and no action_XXX button is |
||
| 1658 | * available, then the first button in the actions list will be pressed. |
||
| 1659 | * However, if this is "delete", for example, this isn't such a good idea. |
||
| 1660 | * |
||
| 1661 | * @return Form |
||
| 1662 | */ |
||
| 1663 | public function disableDefaultAction() |
||
| 1669 | |||
| 1670 | /** |
||
| 1671 | * Disable the requirement of a security token on this form instance. This |
||
| 1672 | * security protects against CSRF attacks, but you should disable this if |
||
| 1673 | * you don't want to tie a form to a session - eg a search form. |
||
| 1674 | * |
||
| 1675 | * Check for token state with {@link getSecurityToken()} and |
||
| 1676 | * {@link SecurityToken->isEnabled()}. |
||
| 1677 | * |
||
| 1678 | * @return Form |
||
| 1679 | */ |
||
| 1680 | public function disableSecurityToken() |
||
| 1686 | |||
| 1687 | /** |
||
| 1688 | * Enable {@link SecurityToken} protection for this form instance. |
||
| 1689 | * |
||
| 1690 | * Check for token state with {@link getSecurityToken()} and |
||
| 1691 | * {@link SecurityToken->isEnabled()}. |
||
| 1692 | * |
||
| 1693 | * @return Form |
||
| 1694 | */ |
||
| 1695 | public function enableSecurityToken() |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Returns the security token for this form (if any exists). |
||
| 1704 | * |
||
| 1705 | * Doesn't check for {@link securityTokenEnabled()}. |
||
| 1706 | * |
||
| 1707 | * Use {@link SecurityToken::inst()} to get a global token. |
||
| 1708 | * |
||
| 1709 | * @return SecurityToken|null |
||
| 1710 | */ |
||
| 1711 | public function getSecurityToken() |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Compiles all CSS-classes. |
||
| 1718 | * |
||
| 1719 | * @return string |
||
| 1720 | */ |
||
| 1721 | public function extraClass() |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Add a CSS-class to the form-container. If needed, multiple classes can |
||
| 1728 | * be added by delimiting a string with spaces. |
||
| 1729 | * |
||
| 1730 | * @param string $class A string containing a classname or several class |
||
| 1731 | * names delimited by a single space. |
||
| 1732 | * @return $this |
||
| 1733 | */ |
||
| 1734 | public function addExtraClass($class) |
||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * Remove a CSS-class from the form-container. Multiple class names can |
||
| 1747 | * be passed through as a space delimited string |
||
| 1748 | * |
||
| 1749 | * @param string $class |
||
| 1750 | * @return $this |
||
| 1751 | */ |
||
| 1752 | public function removeExtraClass($class) |
||
| 1762 | |||
| 1763 | public function debug() |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * Current request handler, build by buildRequestHandler(), |
||
| 1782 | * accessed by getRequestHandler() |
||
| 1783 | * |
||
| 1784 | * @var FormRequestHandler |
||
| 1785 | */ |
||
| 1786 | protected $requestHandler = null; |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Get request handler for this form |
||
| 1790 | * |
||
| 1791 | * @return FormRequestHandler |
||
| 1792 | */ |
||
| 1793 | public function getRequestHandler() |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * Assign a specific request handler for this form |
||
| 1803 | * |
||
| 1804 | * @param FormRequestHandler $handler |
||
| 1805 | * @return $this |
||
| 1806 | */ |
||
| 1807 | public function setRequestHandler(FormRequestHandler $handler) |
||
| 1812 | |||
| 1813 | /** |
||
| 1814 | * Scaffold new request handler for this form |
||
| 1815 | * |
||
| 1816 | * @return FormRequestHandler |
||
| 1817 | */ |
||
| 1818 | protected function buildRequestHandler() |
||
| 1822 | } |
||
| 1823 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: