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 RequestHandler |
||
| 68 | { |
||
| 69 | use FormMessage; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Form submission data is URL encoded |
||
| 73 | */ |
||
| 74 | const ENC_TYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Form submission data is multipart form |
||
| 78 | */ |
||
| 79 | const ENC_TYPE_MULTIPART = 'multipart/form-data'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Accessed by Form.ss; modified by {@link formHtmlContent()}. |
||
| 83 | * A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously |
||
| 84 | * |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | public $IncludeFormTag = true; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var FieldList |
||
| 91 | */ |
||
| 92 | protected $fields; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var FieldList |
||
| 96 | */ |
||
| 97 | protected $actions; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Controller |
||
| 101 | */ |
||
| 102 | protected $controller; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $name; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var Validator |
||
| 111 | */ |
||
| 112 | protected $validator; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var callable {@see setValidationResponseCallback()} |
||
| 116 | */ |
||
| 117 | protected $validationResponseCallback; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | protected $formMethod = "POST"; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var boolean |
||
| 126 | */ |
||
| 127 | protected $strictFormMethodCheck = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var DataObject|null $record Populated by {@link loadDataFrom()}. |
||
| 131 | */ |
||
| 132 | protected $record; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Keeps track of whether this form has a default action or not. |
||
| 136 | * Set to false by $this->disableDefaultAction(); |
||
| 137 | * |
||
| 138 | * @var boolean |
||
| 139 | */ |
||
| 140 | protected $hasDefaultAction = true; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Target attribute of form-tag. |
||
| 144 | * Useful to open a new window upon |
||
| 145 | * form submission. |
||
| 146 | * |
||
| 147 | * @var string|null |
||
| 148 | */ |
||
| 149 | protected $target; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Legend value, to be inserted into the |
||
| 153 | * <legend> element before the <fieldset> |
||
| 154 | * in Form.ss template. |
||
| 155 | * |
||
| 156 | * @var string|null |
||
| 157 | */ |
||
| 158 | protected $legend; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The SS template to render this form HTML into. |
||
| 162 | * Default is "Form", but this can be changed to |
||
| 163 | * another template for customisation. |
||
| 164 | * |
||
| 165 | * @see Form->setTemplate() |
||
| 166 | * @var string|null |
||
| 167 | */ |
||
| 168 | protected $template; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var callable|null |
||
| 172 | */ |
||
| 173 | protected $buttonClickedFunc; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Should we redirect the user back down to the |
||
| 177 | * the form on validation errors rather then just the page |
||
| 178 | * |
||
| 179 | * @var bool |
||
| 180 | */ |
||
| 181 | protected $redirectToFormOnValidationError = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | protected $security = true; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var SecurityToken|null |
||
| 190 | */ |
||
| 191 | protected $securityToken = null; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var array $extraClasses List of additional CSS classes for the form tag. |
||
| 195 | */ |
||
| 196 | protected $extraClasses = array(); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @config |
||
| 200 | * @var array $default_classes The default classes to apply to the Form |
||
| 201 | */ |
||
| 202 | private static $default_classes = array(); |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var string|null |
||
| 206 | */ |
||
| 207 | protected $encType; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var array Any custom form attributes set through {@link setAttributes()}. |
||
| 211 | * Some attributes are calculated on the fly, so please use {@link getAttributes()} to access them. |
||
| 212 | */ |
||
| 213 | protected $attributes = array(); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @var array |
||
| 217 | */ |
||
| 218 | protected $validationExemptActions = array(); |
||
| 219 | |||
| 220 | private static $allowed_actions = array( |
||
| 221 | 'handleField', |
||
| 222 | 'httpSubmission', |
||
| 223 | 'forTemplate', |
||
| 224 | ); |
||
| 225 | |||
| 226 | private static $casting = array( |
||
| 227 | 'AttributesHTML' => 'HTMLFragment', |
||
| 228 | 'FormAttributes' => 'HTMLFragment', |
||
| 229 | 'FormName' => 'Text', |
||
| 230 | 'Legend' => 'HTMLFragment', |
||
| 231 | ); |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @var FormTemplateHelper |
||
| 235 | */ |
||
| 236 | private $templateHelper = null; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @ignore |
||
| 240 | */ |
||
| 241 | private $htmlID = null; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @ignore |
||
| 245 | */ |
||
| 246 | private $formActionPath = false; |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @var bool |
||
| 250 | */ |
||
| 251 | protected $securityTokenAdded = false; |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Create a new form, with the given fields an action buttons. |
||
| 255 | * |
||
| 256 | * @param Controller $controller The parent controller, necessary to create the appropriate form action tag. |
||
| 257 | * @param string $name The method on the controller that will return this form object. |
||
| 258 | * @param FieldList $fields All of the fields in the form - a {@link FieldList} of {@link FormField} objects. |
||
| 259 | * @param FieldList $actions All of the action buttons in the form - a {@link FieldLis} of |
||
| 260 | * {@link FormAction} objects |
||
| 261 | * @param Validator|null $validator Override the default validator instance (Default: {@link RequiredFields}) |
||
| 262 | */ |
||
| 263 | public function __construct($controller, $name, FieldList $fields, FieldList $actions, Validator $validator = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @var array |
||
| 302 | */ |
||
| 303 | private static $url_handlers = array( |
||
| 304 | 'field/$FieldName!' => 'handleField', |
||
| 305 | 'POST ' => 'httpSubmission', |
||
| 306 | 'GET ' => 'httpSubmission', |
||
| 307 | 'HEAD ' => 'httpSubmission', |
||
| 308 | ); |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Load form state from session state |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function restoreFormState() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Flush persistant form state details |
||
| 332 | */ |
||
| 333 | public function clearFormState() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return any form data stored in the session |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | public function getSessionData() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Store the given form data in the session |
||
| 351 | * |
||
| 352 | * @param array $data |
||
| 353 | */ |
||
| 354 | public function setSessionData($data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return any ValidationResult instance stored for this object |
||
| 361 | * |
||
| 362 | * @return ValidationResult The ValidationResult object stored in the session |
||
| 363 | */ |
||
| 364 | public function getSessionValidationResult() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Sets the ValidationResult in the session to be used with the next view of this form. |
||
| 375 | * @param ValidationResult $result The result to save |
||
| 376 | * @param bool $combineWithExisting If true, then this will be added to the existing result. |
||
| 377 | */ |
||
| 378 | public function setSessionValidationResult(ValidationResult $result, $combineWithExisting = false) |
||
| 396 | |||
| 397 | public function clearMessage() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Populate this form with messages from the given ValidationResult. |
||
| 405 | * Note: This will not clear any pre-existing messages |
||
| 406 | * |
||
| 407 | * @param ValidationResult $result |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | public function loadMessagesFrom($result) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set message on a given field name. This message will not persist via redirect. |
||
| 427 | * |
||
| 428 | * @param string $fieldName |
||
| 429 | * @param string $message |
||
| 430 | * @param string $messageType |
||
| 431 | * @param string $messageCast |
||
| 432 | * @return $this |
||
| 433 | */ |
||
| 434 | public function setFieldMessage( |
||
| 446 | |||
| 447 | public function castingHelper($field) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * set up the default classes for the form. This is done on construct so that the default classes can be removed |
||
| 458 | * after instantiation |
||
| 459 | */ |
||
| 460 | protected function setupDefaultClasses() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Handle a form submission. GET and POST requests behave identically. |
||
| 472 | * Populates the form with {@link loadDataFrom()}, calls {@link validate()}, |
||
| 473 | * and only triggers the requested form action/method |
||
| 474 | * if the form is valid. |
||
| 475 | * |
||
| 476 | * @param HTTPRequest $request |
||
| 477 | * @return HTTPResponse |
||
| 478 | * @throws HTTPResponse_Exception |
||
| 479 | */ |
||
| 480 | public function httpSubmission($request) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $action |
||
| 615 | * @return bool |
||
| 616 | */ |
||
| 617 | public function checkAccessAction($action) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return callable |
||
| 641 | */ |
||
| 642 | public function getValidationResponseCallback() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Overrules validation error behaviour in {@link httpSubmission()} |
||
| 649 | * when validation has failed. Useful for optional handling of a certain accepted content type. |
||
| 650 | * |
||
| 651 | * The callback can opt out of handling specific responses by returning NULL, |
||
| 652 | * in which case the default form behaviour will kick in. |
||
| 653 | * |
||
| 654 | * @param $callback |
||
| 655 | * @return self |
||
| 656 | */ |
||
| 657 | public function setValidationResponseCallback($callback) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Returns the appropriate response up the controller chain |
||
| 666 | * if {@link validate()} fails (which is checked prior to executing any form actions). |
||
| 667 | * By default, returns different views for ajax/non-ajax request, and |
||
| 668 | * handles 'application/json' requests with a JSON object containing the error messages. |
||
| 669 | * Behaviour can be influenced by setting {@link $redirectToFormOnValidationError}, |
||
| 670 | * and can be overruled by setting {@link $validationResponseCallback}. |
||
| 671 | * |
||
| 672 | * @param ValidationResult $result |
||
| 673 | * @return HTTPResponse |
||
| 674 | */ |
||
| 675 | protected function getValidationErrorResponse(ValidationResult $result) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Build HTTP error response for ajax requests |
||
| 701 | * |
||
| 702 | * @internal called from {@see Form::getValidationErrorResponse} |
||
| 703 | * @param ValidationResult $result |
||
| 704 | * @return HTTPResponse |
||
| 705 | */ |
||
| 706 | protected function getAjaxErrorResponse(ValidationResult $result) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Get referrer to redirect back to and safely validates it |
||
| 726 | * |
||
| 727 | * @internal called from {@see Form::getValidationErrorResponse} |
||
| 728 | * @return string|null |
||
| 729 | */ |
||
| 730 | protected function getRedirectReferer() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Fields can have action to, let's check if anyone of the responds to $funcname them |
||
| 747 | * |
||
| 748 | * @param SS_List|array $fields |
||
| 749 | * @param callable $funcName |
||
| 750 | * @return FormField |
||
| 751 | */ |
||
| 752 | protected function checkFieldsForAction($fields, $funcName) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Handle a field request. |
||
| 769 | * Uses {@link Form->dataFieldByName()} to find a matching field, |
||
| 770 | * and falls back to {@link FieldList->fieldByName()} to look |
||
| 771 | * for tabs instead. This means that if you have a tab and a |
||
| 772 | * formfield with the same name, this method gives priority |
||
| 773 | * to the formfield. |
||
| 774 | * |
||
| 775 | * @param HTTPRequest $request |
||
| 776 | * @return FormField |
||
| 777 | */ |
||
| 778 | public function handleField($request) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Convert this form into a readonly form |
||
| 792 | */ |
||
| 793 | public function makeReadonly() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Set whether the user should be redirected back down to the |
||
| 800 | * form on the page upon validation errors in the form or if |
||
| 801 | * they just need to redirect back to the page |
||
| 802 | * |
||
| 803 | * @param bool $bool Redirect to form on error? |
||
| 804 | * @return $this |
||
| 805 | */ |
||
| 806 | public function setRedirectToFormOnValidationError($bool) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get whether the user should be redirected back down to the |
||
| 814 | * form on the page upon validation errors |
||
| 815 | * |
||
| 816 | * @return bool |
||
| 817 | */ |
||
| 818 | public function getRedirectToFormOnValidationError() |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @param FormTransformation $trans |
||
| 825 | */ |
||
| 826 | public function transform(FormTransformation $trans) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Get the {@link Validator} attached to this form. |
||
| 849 | * @return Validator |
||
| 850 | */ |
||
| 851 | public function getValidator() |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Set the {@link Validator} on this form. |
||
| 858 | * @param Validator $validator |
||
| 859 | * @return $this |
||
| 860 | */ |
||
| 861 | public function setValidator(Validator $validator) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Remove the {@link Validator} from this from. |
||
| 872 | */ |
||
| 873 | public function unsetValidator() |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Set actions that are exempt from validation |
||
| 881 | * |
||
| 882 | * @param array |
||
| 883 | * @return $this |
||
| 884 | */ |
||
| 885 | public function setValidationExemptActions($actions) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Get a list of actions that are exempt from validation |
||
| 893 | * |
||
| 894 | * @return array |
||
| 895 | */ |
||
| 896 | public function getValidationExemptActions() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Passed a FormAction, returns true if that action is exempt from Form validation |
||
| 903 | * |
||
| 904 | * @param FormAction $action |
||
| 905 | * @return bool |
||
| 906 | */ |
||
| 907 | public function actionIsValidationExempt($action) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Generate extra special fields - namely the security token field (if required). |
||
| 920 | * |
||
| 921 | * @return FieldList |
||
| 922 | */ |
||
| 923 | public function getExtraFields() |
||
| 945 | |||
| 946 | /** |
||
| 947 | * Return the form's fields - used by the templates |
||
| 948 | * |
||
| 949 | * @return FieldList The form fields |
||
| 950 | */ |
||
| 951 | public function Fields() |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Return all <input type="hidden"> fields |
||
| 964 | * in a form - including fields nested in {@link CompositeFields}. |
||
| 965 | * Useful when doing custom field layouts. |
||
| 966 | * |
||
| 967 | * @return FieldList |
||
| 968 | */ |
||
| 969 | public function HiddenFields() |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Return all fields except for the hidden fields. |
||
| 976 | * Useful when making your own simplified form layouts. |
||
| 977 | */ |
||
| 978 | public function VisibleFields() |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Setter for the form fields. |
||
| 985 | * |
||
| 986 | * @param FieldList $fields |
||
| 987 | * @return $this |
||
| 988 | */ |
||
| 989 | public function setFields($fields) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Return the form's action buttons - used by the templates |
||
| 997 | * |
||
| 998 | * @return FieldList The action list |
||
| 999 | */ |
||
| 1000 | public function Actions() |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Setter for the form actions. |
||
| 1007 | * |
||
| 1008 | * @param FieldList $actions |
||
| 1009 | * @return $this |
||
| 1010 | */ |
||
| 1011 | public function setActions($actions) |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Unset all form actions |
||
| 1019 | */ |
||
| 1020 | public function unsetAllActions() |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * @param string $name |
||
| 1028 | * @param string $value |
||
| 1029 | * @return $this |
||
| 1030 | */ |
||
| 1031 | public function setAttribute($name, $value) |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * @param string $name |
||
| 1039 | * @return string |
||
| 1040 | */ |
||
| 1041 | public function getAttribute($name) |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * @return array |
||
| 1051 | */ |
||
| 1052 | public function getAttributes() |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Return the attributes of the form tag - used by the templates. |
||
| 1077 | * |
||
| 1078 | * @param array $attrs Custom attributes to process. Falls back to {@link getAttributes()}. |
||
| 1079 | * If at least one argument is passed as a string, all arguments act as excludes by name. |
||
| 1080 | * |
||
| 1081 | * @return string HTML attributes, ready for insertion into an HTML tag |
||
| 1082 | */ |
||
| 1083 | public function getAttributesHTML($attrs = null) |
||
| 1129 | |||
| 1130 | public function FormAttributes() |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Set the target of this form to any value - useful for opening the form contents in a new window or refreshing |
||
| 1137 | * another frame |
||
| 1138 | * |
||
| 1139 | * @param string|FormTemplateHelper |
||
| 1140 | */ |
||
| 1141 | public function setTemplateHelper($helper) |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Return a {@link FormTemplateHelper} for this form. If one has not been |
||
| 1148 | * set, return the default helper. |
||
| 1149 | * |
||
| 1150 | * @return FormTemplateHelper |
||
| 1151 | */ |
||
| 1152 | public function getTemplateHelper() |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Set the target of this form to any value - useful for opening the form |
||
| 1167 | * contents in a new window or refreshing another frame. |
||
| 1168 | * |
||
| 1169 | * @param string $target The value of the target |
||
| 1170 | * @return $this |
||
| 1171 | */ |
||
| 1172 | public function setTarget($target) |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Set the legend value to be inserted into |
||
| 1181 | * the <legend> element in the Form.ss template. |
||
| 1182 | * @param string $legend |
||
| 1183 | * @return $this |
||
| 1184 | */ |
||
| 1185 | public function setLegend($legend) |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Set the SS template that this form should use |
||
| 1193 | * to render with. The default is "Form". |
||
| 1194 | * |
||
| 1195 | * @param string $template The name of the template (without the .ss extension) |
||
| 1196 | * @return $this |
||
| 1197 | */ |
||
| 1198 | public function setTemplate($template) |
||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Return the template to render this form with. |
||
| 1206 | * |
||
| 1207 | * @return string |
||
| 1208 | */ |
||
| 1209 | public function getTemplate() |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Returs the ordered list of preferred templates for rendering this form |
||
| 1216 | * If the template isn't set, then default to the |
||
| 1217 | * form class name e.g "Form". |
||
| 1218 | * |
||
| 1219 | * @return array |
||
| 1220 | */ |
||
| 1221 | public function getTemplates() |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Returns the encoding type for the form. |
||
| 1233 | * |
||
| 1234 | * By default this will be URL encoded, unless there is a file field present |
||
| 1235 | * in which case multipart is used. You can also set the enc type using |
||
| 1236 | * {@link setEncType}. |
||
| 1237 | */ |
||
| 1238 | public function getEncType() |
||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Sets the form encoding type. The most common encoding types are defined |
||
| 1257 | * in {@link ENC_TYPE_URLENCODED} and {@link ENC_TYPE_MULTIPART}. |
||
| 1258 | * |
||
| 1259 | * @param string $encType |
||
| 1260 | * @return $this |
||
| 1261 | */ |
||
| 1262 | public function setEncType($encType) |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * Returns the real HTTP method for the form: |
||
| 1270 | * GET, POST, PUT, DELETE or HEAD. |
||
| 1271 | * As most browsers only support GET and POST in |
||
| 1272 | * form submissions, all other HTTP methods are |
||
| 1273 | * added as a hidden field "_method" that |
||
| 1274 | * gets evaluated in {@link Director::direct()}. |
||
| 1275 | * See {@link FormMethod()} to get a HTTP method |
||
| 1276 | * for safe insertion into a <form> tag. |
||
| 1277 | * |
||
| 1278 | * @return string HTTP method |
||
| 1279 | */ |
||
| 1280 | public function FormHttpMethod() |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Returns the form method to be used in the <form> tag. |
||
| 1287 | * See {@link FormHttpMethod()} to get the "real" method. |
||
| 1288 | * |
||
| 1289 | * @return string Form HTTP method restricted to 'GET' or 'POST' |
||
| 1290 | */ |
||
| 1291 | public function FormMethod() |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Set the form method: GET, POST, PUT, DELETE. |
||
| 1302 | * |
||
| 1303 | * @param string $method |
||
| 1304 | * @param bool $strict If non-null, pass value to {@link setStrictFormMethodCheck()}. |
||
| 1305 | * @return $this |
||
| 1306 | */ |
||
| 1307 | public function setFormMethod($method, $strict = null) |
||
| 1315 | |||
| 1316 | /** |
||
| 1317 | * If set to true, enforce the matching of the form method. |
||
| 1318 | * |
||
| 1319 | * This will mean two things: |
||
| 1320 | * - GET vars will be ignored by a POST form, and vice versa |
||
| 1321 | * - A submission where the HTTP method used doesn't match the form will return a 400 error. |
||
| 1322 | * |
||
| 1323 | * If set to false (the default), then the form method is only used to construct the default |
||
| 1324 | * form. |
||
| 1325 | * |
||
| 1326 | * @param $bool boolean |
||
| 1327 | * @return $this |
||
| 1328 | */ |
||
| 1329 | public function setStrictFormMethodCheck($bool) |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * @return boolean |
||
| 1337 | */ |
||
| 1338 | public function getStrictFormMethodCheck() |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Return the form's action attribute. |
||
| 1345 | * This is build by adding an executeForm get variable to the parent controller's Link() value |
||
| 1346 | * |
||
| 1347 | * @return string |
||
| 1348 | */ |
||
| 1349 | public function FormAction() |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Set the form action attribute to a custom URL. |
||
| 1362 | * |
||
| 1363 | * Note: For "normal" forms, you shouldn't need to use this method. It is |
||
| 1364 | * recommended only for situations where you have two relatively distinct |
||
| 1365 | * parts of the system trying to communicate via a form post. |
||
| 1366 | * |
||
| 1367 | * @param string $path |
||
| 1368 | * @return $this |
||
| 1369 | */ |
||
| 1370 | public function setFormAction($path) |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Returns the name of the form. |
||
| 1379 | * |
||
| 1380 | * @return string |
||
| 1381 | */ |
||
| 1382 | public function FormName() |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Set the HTML ID attribute of the form. |
||
| 1389 | * |
||
| 1390 | * @param string $id |
||
| 1391 | * @return $this |
||
| 1392 | */ |
||
| 1393 | public function setHTMLID($id) |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * @return string |
||
| 1402 | */ |
||
| 1403 | public function getHTMLID() |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Get the controller. |
||
| 1410 | * |
||
| 1411 | * @return Controller |
||
| 1412 | */ |
||
| 1413 | public function getController() |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Set the controller. |
||
| 1420 | * |
||
| 1421 | * @param Controller $controller |
||
| 1422 | * @return Form |
||
| 1423 | */ |
||
| 1424 | public function setController($controller) |
||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Get the name of the form. |
||
| 1433 | * |
||
| 1434 | * @return string |
||
| 1435 | */ |
||
| 1436 | public function getName() |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Set the name of the form. |
||
| 1443 | * |
||
| 1444 | * @param string $name |
||
| 1445 | * @return Form |
||
| 1446 | */ |
||
| 1447 | public function setName($name) |
||
| 1453 | |||
| 1454 | /** |
||
| 1455 | * Returns an object where there is a method with the same name as each data |
||
| 1456 | * field on the form. |
||
| 1457 | * |
||
| 1458 | * That method will return the field itself. |
||
| 1459 | * |
||
| 1460 | * It means that you can execute $firstName = $form->FieldMap()->FirstName() |
||
| 1461 | */ |
||
| 1462 | public function FieldMap() |
||
| 1466 | |||
| 1467 | /** |
||
| 1468 | * Set a message to the session, for display next time this form is shown. |
||
| 1469 | * |
||
| 1470 | * @param string $message the text of the message |
||
| 1471 | * @param string $type Should be set to good, bad, or warning. |
||
| 1472 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
| 1473 | * Bool values will be treated as plain text flag. |
||
| 1474 | */ |
||
| 1475 | public function sessionMessage($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT) |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Set an error to the session, for display next time this form is shown. |
||
| 1485 | * |
||
| 1486 | * @param string $message the text of the message |
||
| 1487 | * @param string $type Should be set to good, bad, or warning. |
||
| 1488 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
| 1489 | * Bool values will be treated as plain text flag. |
||
| 1490 | */ |
||
| 1491 | public function sessionError($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT) |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Returns the DataObject that has given this form its data |
||
| 1501 | * through {@link loadDataFrom()}. |
||
| 1502 | * |
||
| 1503 | * @return DataObject |
||
| 1504 | */ |
||
| 1505 | public function getRecord() |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Get the legend value to be inserted into the |
||
| 1512 | * <legend> element in Form.ss |
||
| 1513 | * |
||
| 1514 | * @return string |
||
| 1515 | */ |
||
| 1516 | public function getLegend() |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * Processing that occurs before a form is executed. |
||
| 1523 | * |
||
| 1524 | * This includes form validation, if it fails, we throw a ValidationException |
||
| 1525 | * |
||
| 1526 | * This includes form validation, if it fails, we redirect back |
||
| 1527 | * to the form with appropriate error messages. |
||
| 1528 | * Always return true if the current form action is exempt from validation |
||
| 1529 | * |
||
| 1530 | * Triggered through {@link httpSubmission()}. |
||
| 1531 | * |
||
| 1532 | * |
||
| 1533 | * Note that CSRF protection takes place in {@link httpSubmission()}, |
||
| 1534 | * if it fails the form data will never reach this method. |
||
| 1535 | * |
||
| 1536 | * @return ValidationResult |
||
| 1537 | */ |
||
| 1538 | public function validationResult() |
||
| 1556 | |||
| 1557 | const MERGE_DEFAULT = 0; |
||
| 1558 | const MERGE_CLEAR_MISSING = 1; |
||
| 1559 | const MERGE_IGNORE_FALSEISH = 2; |
||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Load data from the given DataObject or array. |
||
| 1563 | * |
||
| 1564 | * It will call $object->MyField to get the value of MyField. |
||
| 1565 | * If you passed an array, it will call $object[MyField]. |
||
| 1566 | * Doesn't save into dataless FormFields ({@link DatalessField}), |
||
| 1567 | * as determined by {@link FieldList->dataFields()}. |
||
| 1568 | * |
||
| 1569 | * By default, if a field isn't set (as determined by isset()), |
||
| 1570 | * its value will not be saved to the field, retaining |
||
| 1571 | * potential existing values. |
||
| 1572 | * |
||
| 1573 | * Passed data should not be escaped, and is saved to the FormField instances unescaped. |
||
| 1574 | * Escaping happens automatically on saving the data through {@link saveInto()}. |
||
| 1575 | * |
||
| 1576 | * Escaping happens automatically on saving the data through |
||
| 1577 | * {@link saveInto()}. |
||
| 1578 | * |
||
| 1579 | * @uses FieldList->dataFields() |
||
| 1580 | * @uses FormField->setValue() |
||
| 1581 | * |
||
| 1582 | * @param array|DataObject $data |
||
| 1583 | * @param int $mergeStrategy |
||
| 1584 | * For every field, {@link $data} is interrogated whether it contains a relevant property/key, and |
||
| 1585 | * what that property/key's value is. |
||
| 1586 | * |
||
| 1587 | * By default, if {@link $data} does contain a property/key, the fields value is always replaced by {@link $data}'s |
||
| 1588 | * value, even if that value is null/false/etc. Fields which don't match any property/key in {@link $data} are |
||
| 1589 | * "left alone", meaning they retain any previous value. |
||
| 1590 | * |
||
| 1591 | * You can pass a bitmask here to change this behaviour. |
||
| 1592 | * |
||
| 1593 | * Passing CLEAR_MISSING means that any fields that don't match any property/key in |
||
| 1594 | * {@link $data} are cleared. |
||
| 1595 | * |
||
| 1596 | * Passing IGNORE_FALSEISH means that any false-ish value in {@link $data} won't replace |
||
| 1597 | * a field's value. |
||
| 1598 | * |
||
| 1599 | * For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing |
||
| 1600 | * CLEAR_MISSING |
||
| 1601 | * |
||
| 1602 | * @param array $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1603 | * form that has some fields that save to one object, and some that save to another. |
||
| 1604 | * @return $this |
||
| 1605 | */ |
||
| 1606 | public function loadDataFrom($data, $mergeStrategy = 0, $fieldList = null) |
||
| 1690 | |||
| 1691 | /** |
||
| 1692 | * Save the contents of this form into the given data object. |
||
| 1693 | * It will make use of setCastedField() to do this. |
||
| 1694 | * |
||
| 1695 | * @param DataObjectInterface $dataObject The object to save data into |
||
| 1696 | * @param FieldList $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1697 | * form that has some fields that save to one object, and some that save to another. |
||
| 1698 | */ |
||
| 1699 | public function saveInto(DataObjectInterface $dataObject, $fieldList = null) |
||
| 1724 | |||
| 1725 | /** |
||
| 1726 | * Get the submitted data from this form through |
||
| 1727 | * {@link FieldList->dataFields()}, which filters out |
||
| 1728 | * any form-specific data like form-actions. |
||
| 1729 | * Calls {@link FormField->dataValue()} on each field, |
||
| 1730 | * which returns a value suitable for insertion into a DataObject |
||
| 1731 | * property. |
||
| 1732 | * |
||
| 1733 | * @return array |
||
| 1734 | */ |
||
| 1735 | public function getData() |
||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * Return a rendered version of this form. |
||
| 1753 | * |
||
| 1754 | * This is returned when you access a form as $FormObject rather |
||
| 1755 | * than <% with FormObject %> |
||
| 1756 | * |
||
| 1757 | * @return DBHTMLText |
||
| 1758 | */ |
||
| 1759 | public function forTemplate() |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Return a rendered version of this form, suitable for ajax post-back. |
||
| 1771 | * |
||
| 1772 | * It triggers slightly different behaviour, such as disabling the rewriting |
||
| 1773 | * of # links. |
||
| 1774 | * |
||
| 1775 | * @return DBHTMLText |
||
| 1776 | */ |
||
| 1777 | public function forAjaxTemplate() |
||
| 1788 | |||
| 1789 | /** |
||
| 1790 | * Returns an HTML rendition of this form, without the <form> tag itself. |
||
| 1791 | * |
||
| 1792 | * Attaches 3 extra hidden files, _form_action, _form_name, _form_method, |
||
| 1793 | * and _form_enctype. These are the attributes of the form. These fields |
||
| 1794 | * can be used to send the form to Ajax. |
||
| 1795 | * |
||
| 1796 | * @deprecated 5.0 |
||
| 1797 | * @return string |
||
| 1798 | */ |
||
| 1799 | public function formHtmlContent() |
||
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Render this form using the given template, and return the result as a string |
||
| 1817 | * You can pass either an SSViewer or a template name |
||
| 1818 | * @param string|array $template |
||
| 1819 | * @return DBHTMLText |
||
| 1820 | */ |
||
| 1821 | public function renderWithoutActionButton($template) |
||
| 1833 | |||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Sets the button that was clicked. This should only be called by the Controller. |
||
| 1837 | * |
||
| 1838 | * @param callable $funcName The name of the action method that will be called. |
||
| 1839 | * @return $this |
||
| 1840 | */ |
||
| 1841 | public function setButtonClicked($funcName) |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * @return FormAction |
||
| 1850 | */ |
||
| 1851 | public function buttonClicked() |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Get a list of all actions, including those in the main "fields" FieldList |
||
| 1865 | * |
||
| 1866 | * @return array |
||
| 1867 | */ |
||
| 1868 | protected function getAllActions() |
||
| 1880 | |||
| 1881 | /** |
||
| 1882 | * Return the default button that should be clicked when another one isn't |
||
| 1883 | * available. |
||
| 1884 | * |
||
| 1885 | * @return FormAction |
||
| 1886 | */ |
||
| 1887 | public function defaultAction() |
||
| 1894 | |||
| 1895 | /** |
||
| 1896 | * Disable the default button. |
||
| 1897 | * |
||
| 1898 | * Ordinarily, when a form is processed and no action_XXX button is |
||
| 1899 | * available, then the first button in the actions list will be pressed. |
||
| 1900 | * However, if this is "delete", for example, this isn't such a good idea. |
||
| 1901 | * |
||
| 1902 | * @return Form |
||
| 1903 | */ |
||
| 1904 | public function disableDefaultAction() |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Disable the requirement of a security token on this form instance. This |
||
| 1913 | * security protects against CSRF attacks, but you should disable this if |
||
| 1914 | * you don't want to tie a form to a session - eg a search form. |
||
| 1915 | * |
||
| 1916 | * Check for token state with {@link getSecurityToken()} and |
||
| 1917 | * {@link SecurityToken->isEnabled()}. |
||
| 1918 | * |
||
| 1919 | * @return Form |
||
| 1920 | */ |
||
| 1921 | public function disableSecurityToken() |
||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * Enable {@link SecurityToken} protection for this form instance. |
||
| 1930 | * |
||
| 1931 | * Check for token state with {@link getSecurityToken()} and |
||
| 1932 | * {@link SecurityToken->isEnabled()}. |
||
| 1933 | * |
||
| 1934 | * @return Form |
||
| 1935 | */ |
||
| 1936 | public function enableSecurityToken() |
||
| 1942 | |||
| 1943 | /** |
||
| 1944 | * Returns the security token for this form (if any exists). |
||
| 1945 | * |
||
| 1946 | * Doesn't check for {@link securityTokenEnabled()}. |
||
| 1947 | * |
||
| 1948 | * Use {@link SecurityToken::inst()} to get a global token. |
||
| 1949 | * |
||
| 1950 | * @return SecurityToken|null |
||
| 1951 | */ |
||
| 1952 | public function getSecurityToken() |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * Compiles all CSS-classes. |
||
| 1959 | * |
||
| 1960 | * @return string |
||
| 1961 | */ |
||
| 1962 | public function extraClass() |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * Add a CSS-class to the form-container. If needed, multiple classes can |
||
| 1969 | * be added by delimiting a string with spaces. |
||
| 1970 | * |
||
| 1971 | * @param string $class A string containing a classname or several class |
||
| 1972 | * names delimited by a single space. |
||
| 1973 | * @return $this |
||
| 1974 | */ |
||
| 1975 | public function addExtraClass($class) |
||
| 1985 | |||
| 1986 | /** |
||
| 1987 | * Remove a CSS-class from the form-container. Multiple class names can |
||
| 1988 | * be passed through as a space delimited string |
||
| 1989 | * |
||
| 1990 | * @param string $class |
||
| 1991 | * @return $this |
||
| 1992 | */ |
||
| 1993 | public function removeExtraClass($class) |
||
| 2003 | |||
| 2004 | public function debug() |
||
| 2019 | |||
| 2020 | |||
| 2021 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 2022 | // TESTING HELPERS |
||
| 2023 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 2024 | |||
| 2025 | /** |
||
| 2026 | * Test a submission of this form. |
||
| 2027 | * @param string $action |
||
| 2028 | * @param array $data |
||
| 2029 | * @return HTTPResponse the response object that the handling controller produces. You can interrogate this in |
||
| 2030 | * your unit test. |
||
| 2031 | * @throws HTTPResponse_Exception |
||
| 2032 | */ |
||
| 2033 | public function testSubmission($action, $data) |
||
| 2039 | |||
| 2040 | /** |
||
| 2041 | * Test an ajax submission of this form. |
||
| 2042 | * |
||
| 2043 | * @param string $action |
||
| 2044 | * @param array $data |
||
| 2045 | * @return HTTPResponse the response object that the handling controller produces. You can interrogate this in |
||
| 2046 | * your unit test. |
||
| 2047 | */ |
||
| 2048 | public function testAjaxSubmission($action, $data) |
||
| 2053 | } |
||
| 2054 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.