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 |
||
| 46 | class Form extends RequestHandler { |
||
| 47 | |||
| 48 | const ENC_TYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
||
| 49 | const ENC_TYPE_MULTIPART = 'multipart/form-data'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean $includeFormTag Accessed by Form.ss; modified by {@link formHtmlContent()}. |
||
| 53 | * A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously |
||
| 54 | */ |
||
| 55 | public $IncludeFormTag = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var FieldList|null |
||
| 59 | */ |
||
| 60 | protected $fields; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var FieldList|null |
||
| 64 | */ |
||
| 65 | protected $actions; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Controller|null |
||
| 69 | */ |
||
| 70 | protected $controller; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string|null |
||
| 74 | */ |
||
| 75 | protected $name; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Validator|null |
||
| 79 | */ |
||
| 80 | protected $validator; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $formMethod = "POST"; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var boolean |
||
| 89 | */ |
||
| 90 | protected $strictFormMethodCheck = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string|null |
||
| 94 | */ |
||
| 95 | protected static $current_action; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var DataObject|null $record Populated by {@link loadDataFrom()}. |
||
| 99 | */ |
||
| 100 | protected $record; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Keeps track of whether this form has a default action or not. |
||
| 104 | * Set to false by $this->disableDefaultAction(); |
||
| 105 | * |
||
| 106 | * @var boolean |
||
| 107 | */ |
||
| 108 | protected $hasDefaultAction = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Target attribute of form-tag. |
||
| 112 | * Useful to open a new window upon |
||
| 113 | * form submission. |
||
| 114 | * |
||
| 115 | * @var string|null |
||
| 116 | */ |
||
| 117 | protected $target; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Legend value, to be inserted into the |
||
| 121 | * <legend> element before the <fieldset> |
||
| 122 | * in Form.ss template. |
||
| 123 | * |
||
| 124 | * @var string|null |
||
| 125 | */ |
||
| 126 | protected $legend; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The SS template to render this form HTML into. |
||
| 130 | * Default is "Form", but this can be changed to |
||
| 131 | * another template for customisation. |
||
| 132 | * |
||
| 133 | * @see Form->setTemplate() |
||
| 134 | * @var string|null |
||
| 135 | */ |
||
| 136 | protected $template; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var callable|null |
||
| 140 | */ |
||
| 141 | protected $buttonClickedFunc; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string|null |
||
| 145 | */ |
||
| 146 | protected $message; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string|null |
||
| 150 | */ |
||
| 151 | protected $messageType; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Should we redirect the user back down to the |
||
| 155 | * the form on validation errors rather then just the page |
||
| 156 | * |
||
| 157 | * @var bool |
||
| 158 | */ |
||
| 159 | protected $redirectToFormOnValidationError = false; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | protected $security = true; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var SecurityToken|null |
||
| 168 | */ |
||
| 169 | protected $securityToken = null; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var array $extraClasses List of additional CSS classes for the form tag. |
||
| 173 | */ |
||
| 174 | protected $extraClasses = array(); |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @config |
||
| 178 | * @var array $default_classes The default classes to apply to the Form |
||
| 179 | */ |
||
| 180 | private static $default_classes = array(); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var string|null |
||
| 184 | */ |
||
| 185 | protected $encType; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var array Any custom form attributes set through {@link setAttributes()}. |
||
| 189 | * Some attributes are calculated on the fly, so please use {@link getAttributes()} to access them. |
||
| 190 | */ |
||
| 191 | protected $attributes = array(); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | private static $allowed_actions = array( |
||
| 197 | 'handleField', |
||
| 198 | 'httpSubmission', |
||
| 199 | 'forTemplate', |
||
| 200 | ); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var FormTemplateHelper |
||
| 204 | */ |
||
| 205 | private $templateHelper = null; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @ignore |
||
| 209 | */ |
||
| 210 | private $htmlID = null; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @ignore |
||
| 214 | */ |
||
| 215 | private $formActionPath = false; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @var bool |
||
| 219 | */ |
||
| 220 | protected $securityTokenAdded = false; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Create a new form, with the given fields an action buttons. |
||
| 224 | * |
||
| 225 | * @param Controller $controller The parent controller, necessary to create the appropriate form action tag. |
||
| 226 | * @param string $name The method on the controller that will return this form object. |
||
| 227 | * @param FieldList $fields All of the fields in the form - a {@link FieldList} of {@link FormField} objects. |
||
| 228 | * @param FieldList $actions All of the action buttons in the form - a {@link FieldLis} of |
||
| 229 | * {@link FormAction} objects |
||
| 230 | * @param Validator $validator Override the default validator instance (Default: {@link RequiredFields}) |
||
| 231 | */ |
||
| 232 | public function __construct($controller, $name, FieldList $fields, FieldList $actions, $validator = null) { |
||
| 233 | parent::__construct(); |
||
| 234 | |||
| 235 | if(!$fields instanceof FieldList) { |
||
| 236 | throw new InvalidArgumentException('$fields must be a valid FieldList instance'); |
||
| 237 | } |
||
| 238 | if(!$actions instanceof FieldList) { |
||
| 239 | throw new InvalidArgumentException('$actions must be a valid FieldList instance'); |
||
| 240 | } |
||
| 241 | if($validator && !$validator instanceof Validator) { |
||
| 242 | throw new InvalidArgumentException('$validator must be a Validator instance'); |
||
| 243 | } |
||
| 244 | |||
| 245 | $fields->setForm($this); |
||
| 246 | $actions->setForm($this); |
||
| 247 | |||
| 248 | $this->fields = $fields; |
||
| 249 | $this->actions = $actions; |
||
| 250 | $this->controller = $controller; |
||
| 251 | $this->name = $name; |
||
| 252 | |||
| 253 | if(!$this->controller) user_error("$this->class form created without a controller", E_USER_ERROR); |
||
| 254 | |||
| 255 | // Form validation |
||
| 256 | $this->validator = ($validator) ? $validator : new RequiredFields(); |
||
| 257 | $this->validator->setForm($this); |
||
| 258 | |||
| 259 | // Form error controls |
||
| 260 | $this->setupFormErrors(); |
||
| 261 | |||
| 262 | // Check if CSRF protection is enabled, either on the parent controller or from the default setting. Note that |
||
| 263 | // method_exists() is used as some controllers (e.g. GroupTest) do not always extend from Object. |
||
| 264 | if(method_exists($controller, 'securityTokenEnabled') || (method_exists($controller, 'hasMethod') |
||
| 265 | && $controller->hasMethod('securityTokenEnabled'))) { |
||
| 266 | |||
| 267 | $securityEnabled = $controller->securityTokenEnabled(); |
||
| 268 | } else { |
||
| 269 | $securityEnabled = SecurityToken::is_enabled(); |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->securityToken = ($securityEnabled) ? new SecurityToken() : new NullSecurityToken(); |
||
| 273 | |||
| 274 | $this->setupDefaultClasses(); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @var array |
||
| 279 | */ |
||
| 280 | private static $url_handlers = array( |
||
| 281 | 'field/$FieldName!' => 'handleField', |
||
| 282 | 'POST ' => 'httpSubmission', |
||
| 283 | 'GET ' => 'httpSubmission', |
||
| 284 | 'HEAD ' => 'httpSubmission', |
||
| 285 | ); |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set up current form errors in session to |
||
| 289 | * the current form if appropriate. |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function setupFormErrors() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * set up the default classes for the form. This is done on construct so that the default classes can be removed |
||
| 321 | * after instantiation |
||
| 322 | */ |
||
| 323 | protected function setupDefaultClasses() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Handle a form submission. GET and POST requests behave identically. |
||
| 334 | * Populates the form with {@link loadDataFrom()}, calls {@link validate()}, |
||
| 335 | * and only triggers the requested form action/method |
||
| 336 | * if the form is valid. |
||
| 337 | * |
||
| 338 | * @param SS_HTTPRequest $request |
||
| 339 | * @throws SS_HTTPResponse_Exception |
||
| 340 | */ |
||
| 341 | public function httpSubmission($request) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $action |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function checkAccessAction($action) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns the appropriate response up the controller chain |
||
| 504 | * if {@link validate()} fails (which is checked prior to executing any form actions). |
||
| 505 | * By default, returns different views for ajax/non-ajax request, and |
||
| 506 | * handles 'application/json' requests with a JSON object containing the error messages. |
||
| 507 | * Behaviour can be influenced by setting {@link $redirectToFormOnValidationError}. |
||
| 508 | * |
||
| 509 | * @return SS_HTTPResponse|string |
||
| 510 | */ |
||
| 511 | protected function getValidationErrorResponse() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Fields can have action to, let's check if anyone of the responds to $funcname them |
||
| 546 | * |
||
| 547 | * @param SS_List|array $fields |
||
| 548 | * @param callable $funcName |
||
| 549 | * @return FormField |
||
| 550 | */ |
||
| 551 | protected function checkFieldsForAction($fields, $funcName) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Handle a field request. |
||
| 565 | * Uses {@link Form->dataFieldByName()} to find a matching field, |
||
| 566 | * and falls back to {@link FieldList->fieldByName()} to look |
||
| 567 | * for tabs instead. This means that if you have a tab and a |
||
| 568 | * formfield with the same name, this method gives priority |
||
| 569 | * to the formfield. |
||
| 570 | * |
||
| 571 | * @param SS_HTTPRequest $request |
||
| 572 | * @return FormField |
||
| 573 | */ |
||
| 574 | public function handleField($request) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Convert this form into a readonly form |
||
| 587 | */ |
||
| 588 | public function makeReadonly() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Set whether the user should be redirected back down to the |
||
| 594 | * form on the page upon validation errors in the form or if |
||
| 595 | * they just need to redirect back to the page |
||
| 596 | * |
||
| 597 | * @param bool $bool Redirect to form on error? |
||
| 598 | * @return $this |
||
| 599 | */ |
||
| 600 | public function setRedirectToFormOnValidationError($bool) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get whether the user should be redirected back down to the |
||
| 607 | * form on the page upon validation errors |
||
| 608 | * |
||
| 609 | * @return bool |
||
| 610 | */ |
||
| 611 | public function getRedirectToFormOnValidationError() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Add a plain text error message to a field on this form. It will be saved into the session |
||
| 617 | * and used the next time this form is displayed. |
||
| 618 | * @param string $fieldName |
||
| 619 | * @param string $message |
||
| 620 | * @param string $messageType |
||
| 621 | * @param bool $escapeHtml |
||
| 622 | */ |
||
| 623 | public function addErrorMessage($fieldName, $message, $messageType, $escapeHtml = true) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @param FormTransformation $trans |
||
| 633 | */ |
||
| 634 | public function transform(FormTransformation $trans) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Get the {@link Validator} attached to this form. |
||
| 655 | * @return Validator |
||
| 656 | */ |
||
| 657 | public function getValidator() { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Set the {@link Validator} on this form. |
||
| 663 | * @param Validator $validator |
||
| 664 | * @return $this |
||
| 665 | */ |
||
| 666 | public function setValidator(Validator $validator ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Remove the {@link Validator} from this from. |
||
| 676 | */ |
||
| 677 | public function unsetValidator(){ |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Convert this form to another format. |
||
| 684 | * @param FormTransformation $format |
||
| 685 | */ |
||
| 686 | public function transformTo(FormTransformation $format) { |
||
| 697 | |||
| 698 | |||
| 699 | /** |
||
| 700 | * Generate extra special fields - namely the security token field (if required). |
||
| 701 | * |
||
| 702 | * @return FieldList |
||
| 703 | */ |
||
| 704 | public function getExtraFields() { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Return the form's fields - used by the templates |
||
| 726 | * |
||
| 727 | * @return FieldList The form fields |
||
| 728 | */ |
||
| 729 | public function Fields() { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Return all <input type="hidden"> fields |
||
| 739 | * in a form - including fields nested in {@link CompositeFields}. |
||
| 740 | * Useful when doing custom field layouts. |
||
| 741 | * |
||
| 742 | * @return FieldList |
||
| 743 | */ |
||
| 744 | public function HiddenFields() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Return all fields except for the hidden fields. |
||
| 750 | * Useful when making your own simplified form layouts. |
||
| 751 | */ |
||
| 752 | public function VisibleFields() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Setter for the form fields. |
||
| 758 | * |
||
| 759 | * @param FieldList $fields |
||
| 760 | * @return $this |
||
| 761 | */ |
||
| 762 | public function setFields($fields) { |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Return the form's action buttons - used by the templates |
||
| 769 | * |
||
| 770 | * @return FieldList The action list |
||
| 771 | */ |
||
| 772 | public function Actions() { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Setter for the form actions. |
||
| 778 | * |
||
| 779 | * @param FieldList $actions |
||
| 780 | * @return $this |
||
| 781 | */ |
||
| 782 | public function setActions($actions) { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Unset all form actions |
||
| 789 | */ |
||
| 790 | public function unsetAllActions(){ |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @param string $name |
||
| 797 | * @param string $value |
||
| 798 | * @return $this |
||
| 799 | */ |
||
| 800 | public function setAttribute($name, $value) { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @return string $name |
||
| 807 | */ |
||
| 808 | public function getAttribute($name) { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * @return array |
||
| 814 | */ |
||
| 815 | public function getAttributes() { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Return the attributes of the form tag - used by the templates. |
||
| 837 | * |
||
| 838 | * @param array Custom attributes to process. Falls back to {@link getAttributes()}. |
||
| 839 | * If at least one argument is passed as a string, all arguments act as excludes by name. |
||
| 840 | * |
||
| 841 | * @return string HTML attributes, ready for insertion into an HTML tag |
||
| 842 | */ |
||
| 843 | public function getAttributesHTML($attrs = null) { |
||
| 884 | |||
| 885 | public function FormAttributes() { |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Set the target of this form to any value - useful for opening the form contents in a new window or refreshing |
||
| 891 | * another frame |
||
| 892 | * |
||
| 893 | * @param string|FormTemplateHelper |
||
| 894 | */ |
||
| 895 | public function setTemplateHelper($helper) { |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Return a {@link FormTemplateHelper} for this form. If one has not been |
||
| 901 | * set, return the default helper. |
||
| 902 | * |
||
| 903 | * @return FormTemplateHelper |
||
| 904 | */ |
||
| 905 | public function getTemplateHelper() { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Set the target of this form to any value - useful for opening the form |
||
| 919 | * contents in a new window or refreshing another frame. |
||
| 920 | * |
||
| 921 | * @param target $target The value of the target |
||
| 922 | * @return $this |
||
| 923 | */ |
||
| 924 | public function setTarget($target) { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Set the legend value to be inserted into |
||
| 932 | * the <legend> element in the Form.ss template. |
||
| 933 | * @param string $legend |
||
| 934 | * @return $this |
||
| 935 | */ |
||
| 936 | public function setLegend($legend) { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Set the SS template that this form should use |
||
| 943 | * to render with. The default is "Form". |
||
| 944 | * |
||
| 945 | * @param string $template The name of the template (without the .ss extension) |
||
| 946 | * @return $this |
||
| 947 | */ |
||
| 948 | public function setTemplate($template) { |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Return the template to render this form with. |
||
| 955 | * If the template isn't set, then default to the |
||
| 956 | * form class name e.g "Form". |
||
| 957 | * |
||
| 958 | * @return string |
||
| 959 | */ |
||
| 960 | public function getTemplate() { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Returns the encoding type for the form. |
||
| 967 | * |
||
| 968 | * By default this will be URL encoded, unless there is a file field present |
||
| 969 | * in which case multipart is used. You can also set the enc type using |
||
| 970 | * {@link setEncType}. |
||
| 971 | */ |
||
| 972 | public function getEncType() { |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Sets the form encoding type. The most common encoding types are defined |
||
| 988 | * in {@link ENC_TYPE_URLENCODED} and {@link ENC_TYPE_MULTIPART}. |
||
| 989 | * |
||
| 990 | * @param string $encType |
||
| 991 | * @return $this |
||
| 992 | */ |
||
| 993 | public function setEncType($encType) { |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Returns the real HTTP method for the form: |
||
| 1000 | * GET, POST, PUT, DELETE or HEAD. |
||
| 1001 | * As most browsers only support GET and POST in |
||
| 1002 | * form submissions, all other HTTP methods are |
||
| 1003 | * added as a hidden field "_method" that |
||
| 1004 | * gets evaluated in {@link Director::direct()}. |
||
| 1005 | * See {@link FormMethod()} to get a HTTP method |
||
| 1006 | * for safe insertion into a <form> tag. |
||
| 1007 | * |
||
| 1008 | * @return string HTTP method |
||
| 1009 | */ |
||
| 1010 | public function FormHttpMethod() { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Returns the form method to be used in the <form> tag. |
||
| 1016 | * See {@link FormHttpMethod()} to get the "real" method. |
||
| 1017 | * |
||
| 1018 | * @return string Form HTTP method restricted to 'GET' or 'POST' |
||
| 1019 | */ |
||
| 1020 | public function FormMethod() { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Set the form method: GET, POST, PUT, DELETE. |
||
| 1030 | * |
||
| 1031 | * @param string $method |
||
| 1032 | * @param bool $strict If non-null, pass value to {@link setStrictFormMethodCheck()}. |
||
| 1033 | * @return $this |
||
| 1034 | */ |
||
| 1035 | public function setFormMethod($method, $strict = null) { |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * If set to true, enforce the matching of the form method. |
||
| 1043 | * |
||
| 1044 | * This will mean two things: |
||
| 1045 | * - GET vars will be ignored by a POST form, and vice versa |
||
| 1046 | * - A submission where the HTTP method used doesn't match the form will return a 400 error. |
||
| 1047 | * |
||
| 1048 | * If set to false (the default), then the form method is only used to construct the default |
||
| 1049 | * form. |
||
| 1050 | * |
||
| 1051 | * @param $bool boolean |
||
| 1052 | * @return $this |
||
| 1053 | */ |
||
| 1054 | public function setStrictFormMethodCheck($bool) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * @return boolean |
||
| 1061 | */ |
||
| 1062 | public function getStrictFormMethodCheck() { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Return the form's action attribute. |
||
| 1068 | * This is build by adding an executeForm get variable to the parent controller's Link() value |
||
| 1069 | * |
||
| 1070 | * @return string |
||
| 1071 | */ |
||
| 1072 | public function FormAction() { |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Set the form action attribute to a custom URL. |
||
| 1091 | * |
||
| 1092 | * Note: For "normal" forms, you shouldn't need to use this method. It is |
||
| 1093 | * recommended only for situations where you have two relatively distinct |
||
| 1094 | * parts of the system trying to communicate via a form post. |
||
| 1095 | * |
||
| 1096 | * @param string $path |
||
| 1097 | * @return $this |
||
| 1098 | */ |
||
| 1099 | public function setFormAction($path) { |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Returns the name of the form. |
||
| 1107 | * |
||
| 1108 | * @return string |
||
| 1109 | */ |
||
| 1110 | public function FormName() { |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Set the HTML ID attribute of the form. |
||
| 1116 | * |
||
| 1117 | * @param string $id |
||
| 1118 | * @return $this |
||
| 1119 | */ |
||
| 1120 | public function setHTMLID($id) { |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * @return string |
||
| 1128 | */ |
||
| 1129 | public function getHTMLID() { |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Returns this form's controller. |
||
| 1135 | * |
||
| 1136 | * @return Controller |
||
| 1137 | * @deprecated 4.0 |
||
| 1138 | */ |
||
| 1139 | public function Controller() { |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Get the controller. |
||
| 1147 | * |
||
| 1148 | * @return Controller |
||
| 1149 | */ |
||
| 1150 | public function getController() { |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Set the controller. |
||
| 1156 | * |
||
| 1157 | * @param Controller $controller |
||
| 1158 | * @return Form |
||
| 1159 | */ |
||
| 1160 | public function setController($controller) { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Get the name of the form. |
||
| 1168 | * |
||
| 1169 | * @return string |
||
| 1170 | */ |
||
| 1171 | public function getName() { |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Set the name of the form. |
||
| 1177 | * |
||
| 1178 | * @param string $name |
||
| 1179 | * @return Form |
||
| 1180 | */ |
||
| 1181 | public function setName($name) { |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Returns an object where there is a method with the same name as each data |
||
| 1189 | * field on the form. |
||
| 1190 | * |
||
| 1191 | * That method will return the field itself. |
||
| 1192 | * |
||
| 1193 | * It means that you can execute $firstName = $form->FieldMap()->FirstName() |
||
| 1194 | */ |
||
| 1195 | public function FieldMap() { |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * The next functions store and modify the forms |
||
| 1201 | * message attributes. messages are stored in session under |
||
| 1202 | * $_SESSION[formname][message]; |
||
| 1203 | * |
||
| 1204 | * @return string |
||
| 1205 | */ |
||
| 1206 | public function Message() { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * @return string |
||
| 1214 | */ |
||
| 1215 | public function MessageType() { |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * @return string |
||
| 1223 | */ |
||
| 1224 | protected function getMessageFromSession() { |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Set a status message for the form. |
||
| 1237 | * |
||
| 1238 | * @param string $message the text of the message |
||
| 1239 | * @param string $type Should be set to good, bad, or warning. |
||
| 1240 | * @param boolean $escapeHtml Automatically sanitize the message. Set to FALSE if the message contains HTML. |
||
| 1241 | * In that case, you might want to use {@link Convert::raw2xml()} to escape any |
||
| 1242 | * user supplied data in the message. |
||
| 1243 | * @return $this |
||
| 1244 | */ |
||
| 1245 | public function setMessage($message, $type, $escapeHtml = true) { |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Set a message to the session, for display next time this form is shown. |
||
| 1253 | * |
||
| 1254 | * @param string $message the text of the message |
||
| 1255 | * @param string $type Should be set to good, bad, or warning. |
||
| 1256 | * @param boolean $escapeHtml Automatically sanitize the message. Set to FALSE if the message contains HTML. |
||
| 1257 | * In that case, you might want to use {@link Convert::raw2xml()} to escape any |
||
| 1258 | * user supplied data in the message. |
||
| 1259 | */ |
||
| 1260 | public function sessionMessage($message, $type, $escapeHtml = true) { |
||
| 1267 | |||
| 1268 | public static function messageForForm($formName, $message, $type, $escapeHtml = true) { |
||
| 1275 | |||
| 1276 | public function clearMessage() { |
||
| 1282 | |||
| 1283 | public function resetValidation() { |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Returns the DataObject that has given this form its data |
||
| 1290 | * through {@link loadDataFrom()}. |
||
| 1291 | * |
||
| 1292 | * @return DataObject |
||
| 1293 | */ |
||
| 1294 | public function getRecord() { |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Get the legend value to be inserted into the |
||
| 1300 | * <legend> element in Form.ss |
||
| 1301 | * |
||
| 1302 | * @return string |
||
| 1303 | */ |
||
| 1304 | public function getLegend() { |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Processing that occurs before a form is executed. |
||
| 1310 | * |
||
| 1311 | * This includes form validation, if it fails, we redirect back |
||
| 1312 | * to the form with appropriate error messages. |
||
| 1313 | * |
||
| 1314 | * Triggered through {@link httpSubmission()}. |
||
| 1315 | * |
||
| 1316 | * Note that CSRF protection takes place in {@link httpSubmission()}, |
||
| 1317 | * if it fails the form data will never reach this method. |
||
| 1318 | * |
||
| 1319 | * @return boolean |
||
| 1320 | */ |
||
| 1321 | public function validate(){ |
||
| 1350 | |||
| 1351 | const MERGE_DEFAULT = 0; |
||
| 1352 | const MERGE_CLEAR_MISSING = 1; |
||
| 1353 | const MERGE_IGNORE_FALSEISH = 2; |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Load data from the given DataObject or array. |
||
| 1357 | * |
||
| 1358 | * It will call $object->MyField to get the value of MyField. |
||
| 1359 | * If you passed an array, it will call $object[MyField]. |
||
| 1360 | * Doesn't save into dataless FormFields ({@link DatalessField}), |
||
| 1361 | * as determined by {@link FieldList->dataFields()}. |
||
| 1362 | * |
||
| 1363 | * By default, if a field isn't set (as determined by isset()), |
||
| 1364 | * its value will not be saved to the field, retaining |
||
| 1365 | * potential existing values. |
||
| 1366 | * |
||
| 1367 | * Passed data should not be escaped, and is saved to the FormField instances unescaped. |
||
| 1368 | * Escaping happens automatically on saving the data through {@link saveInto()}. |
||
| 1369 | * |
||
| 1370 | * Escaping happens automatically on saving the data through |
||
| 1371 | * {@link saveInto()}. |
||
| 1372 | * |
||
| 1373 | * @uses FieldList->dataFields() |
||
| 1374 | * @uses FormField->setValue() |
||
| 1375 | * |
||
| 1376 | * @param array|DataObject $data |
||
| 1377 | * @param int $mergeStrategy |
||
| 1378 | * For every field, {@link $data} is interrogated whether it contains a relevant property/key, and |
||
| 1379 | * what that property/key's value is. |
||
| 1380 | * |
||
| 1381 | * By default, if {@link $data} does contain a property/key, the fields value is always replaced by {@link $data}'s |
||
| 1382 | * value, even if that value is null/false/etc. Fields which don't match any property/key in {@link $data} are |
||
| 1383 | * "left alone", meaning they retain any previous value. |
||
| 1384 | * |
||
| 1385 | * You can pass a bitmask here to change this behaviour. |
||
| 1386 | * |
||
| 1387 | * Passing CLEAR_MISSING means that any fields that don't match any property/key in |
||
| 1388 | * {@link $data} are cleared. |
||
| 1389 | * |
||
| 1390 | * Passing IGNORE_FALSEISH means that any false-ish value in {@link $data} won't replace |
||
| 1391 | * a field's value. |
||
| 1392 | * |
||
| 1393 | * For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing |
||
| 1394 | * CLEAR_MISSING |
||
| 1395 | * |
||
| 1396 | * @param array $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1397 | * form that has some fields that save to one object, and some that save to another. |
||
| 1398 | * @return Form |
||
| 1399 | */ |
||
| 1400 | public function loadDataFrom($data, $mergeStrategy = 0, $fieldList = null) { |
||
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Save the contents of this form into the given data object. |
||
| 1497 | * It will make use of setCastedField() to do this. |
||
| 1498 | * |
||
| 1499 | * @param DataObjectInterface $dataObject The object to save data into |
||
| 1500 | * @param FieldList $fieldList An optional list of fields to process. This can be useful when you have a |
||
| 1501 | * form that has some fields that save to one object, and some that save to another. |
||
| 1502 | */ |
||
| 1503 | public function saveInto(DataObjectInterface $dataObject, $fieldList = null) { |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Get the submitted data from this form through |
||
| 1526 | * {@link FieldList->dataFields()}, which filters out |
||
| 1527 | * any form-specific data like form-actions. |
||
| 1528 | * Calls {@link FormField->dataValue()} on each field, |
||
| 1529 | * which returns a value suitable for insertion into a DataObject |
||
| 1530 | * property. |
||
| 1531 | * |
||
| 1532 | * @return array |
||
| 1533 | */ |
||
| 1534 | public function getData() { |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Call the given method on the given field. |
||
| 1551 | * |
||
| 1552 | * @param array $data |
||
| 1553 | * @return mixed |
||
| 1554 | */ |
||
| 1555 | public function callfieldmethod($data) { |
||
| 1577 | |||
| 1578 | /** |
||
| 1579 | * Return a rendered version of this form. |
||
| 1580 | * |
||
| 1581 | * This is returned when you access a form as $FormObject rather |
||
| 1582 | * than <% with FormObject %> |
||
| 1583 | * |
||
| 1584 | * @return HTML |
||
| 1585 | */ |
||
| 1586 | public function forTemplate() { |
||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * Return a rendered version of this form, suitable for ajax post-back. |
||
| 1600 | * |
||
| 1601 | * It triggers slightly different behaviour, such as disabling the rewriting |
||
| 1602 | * of # links. |
||
| 1603 | * |
||
| 1604 | * @return HTML |
||
| 1605 | */ |
||
| 1606 | public function forAjaxTemplate() { |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * Returns an HTML rendition of this form, without the <form> tag itself. |
||
| 1622 | * |
||
| 1623 | * Attaches 3 extra hidden files, _form_action, _form_name, _form_method, |
||
| 1624 | * and _form_enctype. These are the attributes of the form. These fields |
||
| 1625 | * can be used to send the form to Ajax. |
||
| 1626 | * |
||
| 1627 | * @return HTML |
||
| 1628 | */ |
||
| 1629 | public function formHtmlContent() { |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Render this form using the given template, and return the result as a string |
||
| 1645 | * You can pass either an SSViewer or a template name |
||
| 1646 | * @param string|array $template |
||
| 1647 | * @return HTMLText |
||
| 1648 | */ |
||
| 1649 | public function renderWithoutActionButton($template) { |
||
| 1660 | |||
| 1661 | |||
| 1662 | /** |
||
| 1663 | * Sets the button that was clicked. This should only be called by the Controller. |
||
| 1664 | * |
||
| 1665 | * @param callable $funcName The name of the action method that will be called. |
||
| 1666 | * @return $this |
||
| 1667 | */ |
||
| 1668 | public function setButtonClicked($funcName) { |
||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * @return FormAction |
||
| 1676 | */ |
||
| 1677 | public function buttonClicked() { |
||
| 1687 | |||
| 1688 | /** |
||
| 1689 | * Get a list of all actions, including those in the main "fields" FieldList |
||
| 1690 | * |
||
| 1691 | * @return array |
||
| 1692 | */ |
||
| 1693 | protected function getAllActions() { |
||
| 1704 | |||
| 1705 | /** |
||
| 1706 | * Return the default button that should be clicked when another one isn't |
||
| 1707 | * available. |
||
| 1708 | * |
||
| 1709 | * @return FormAction |
||
| 1710 | */ |
||
| 1711 | public function defaultAction() { |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * Disable the default button. |
||
| 1719 | * |
||
| 1720 | * Ordinarily, when a form is processed and no action_XXX button is |
||
| 1721 | * available, then the first button in the actions list will be pressed. |
||
| 1722 | * However, if this is "delete", for example, this isn't such a good idea. |
||
| 1723 | * |
||
| 1724 | * @return Form |
||
| 1725 | */ |
||
| 1726 | public function disableDefaultAction() { |
||
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Disable the requirement of a security token on this form instance. This |
||
| 1734 | * security protects against CSRF attacks, but you should disable this if |
||
| 1735 | * you don't want to tie a form to a session - eg a search form. |
||
| 1736 | * |
||
| 1737 | * Check for token state with {@link getSecurityToken()} and |
||
| 1738 | * {@link SecurityToken->isEnabled()}. |
||
| 1739 | * |
||
| 1740 | * @return Form |
||
| 1741 | */ |
||
| 1742 | public function disableSecurityToken() { |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Enable {@link SecurityToken} protection for this form instance. |
||
| 1750 | * |
||
| 1751 | * Check for token state with {@link getSecurityToken()} and |
||
| 1752 | * {@link SecurityToken->isEnabled()}. |
||
| 1753 | * |
||
| 1754 | * @return Form |
||
| 1755 | */ |
||
| 1756 | public function enableSecurityToken() { |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Returns the security token for this form (if any exists). |
||
| 1764 | * |
||
| 1765 | * Doesn't check for {@link securityTokenEnabled()}. |
||
| 1766 | * |
||
| 1767 | * Use {@link SecurityToken::inst()} to get a global token. |
||
| 1768 | * |
||
| 1769 | * @return SecurityToken|null |
||
| 1770 | */ |
||
| 1771 | public function getSecurityToken() { |
||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Returns the name of a field, if that's the only field that the current |
||
| 1777 | * controller is interested in. |
||
| 1778 | * |
||
| 1779 | * It checks for a call to the callfieldmethod action. |
||
| 1780 | * |
||
| 1781 | * @return string |
||
| 1782 | */ |
||
| 1783 | public static function single_field_required() { |
||
| 1788 | |||
| 1789 | /** |
||
| 1790 | * Return the current form action being called, if available. |
||
| 1791 | * |
||
| 1792 | * @return string |
||
| 1793 | */ |
||
| 1794 | public static function current_action() { |
||
| 1797 | |||
| 1798 | /** |
||
| 1799 | * Set the current form action. Should only be called by {@link Controller}. |
||
| 1800 | * |
||
| 1801 | * @param string $action |
||
| 1802 | */ |
||
| 1803 | public static function set_current_action($action) { |
||
| 1806 | |||
| 1807 | /** |
||
| 1808 | * Compiles all CSS-classes. |
||
| 1809 | * |
||
| 1810 | * @return string |
||
| 1811 | */ |
||
| 1812 | public function extraClass() { |
||
| 1815 | |||
| 1816 | /** |
||
| 1817 | * Add a CSS-class to the form-container. If needed, multiple classes can |
||
| 1818 | * be added by delimiting a string with spaces. |
||
| 1819 | * |
||
| 1820 | * @param string $class A string containing a classname or several class |
||
| 1821 | * names delimited by a single space. |
||
| 1822 | * @return $this |
||
| 1823 | */ |
||
| 1824 | public function addExtraClass($class) { |
||
| 1833 | |||
| 1834 | /** |
||
| 1835 | * Remove a CSS-class from the form-container. Multiple class names can |
||
| 1836 | * be passed through as a space delimited string |
||
| 1837 | * |
||
| 1838 | * @param string $class |
||
| 1839 | * @return $this |
||
| 1840 | */ |
||
| 1841 | public function removeExtraClass($class) { |
||
| 1850 | |||
| 1851 | public function debug() { |
||
| 1863 | |||
| 1864 | |||
| 1865 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 1866 | // TESTING HELPERS |
||
| 1867 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 1868 | |||
| 1869 | /** |
||
| 1870 | * Test a submission of this form. |
||
| 1871 | * @param string $action |
||
| 1872 | * @param array $data |
||
| 1873 | * @return SS_HTTPResponse the response object that the handling controller produces. You can interrogate this in |
||
| 1874 | * your unit test. |
||
| 1875 | * @throws SS_HTTPResponse_Exception |
||
| 1876 | */ |
||
| 1877 | public function testSubmission($action, $data) { |
||
| 1882 | |||
| 1883 | /** |
||
| 1884 | * Test an ajax submission of this form. |
||
| 1885 | * |
||
| 1886 | * @param string $action |
||
| 1887 | * @param array $data |
||
| 1888 | * @return SS_HTTPResponse the response object that the handling controller produces. You can interrogate this in |
||
| 1889 | * your unit test. |
||
| 1890 | */ |
||
| 1891 | public function testAjaxSubmission($action, $data) { |
||
| 1895 | } |
||
| 1896 | |||
| 1923 |
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.