Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FormField 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 FormField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class FormField extends RequestHandler |
||
| 42 | { |
||
| 43 | use FormMessage; |
||
| 44 | |||
| 45 | /** @see $schemaDataType */ |
||
| 46 | const SCHEMA_DATA_TYPE_STRING = 'String'; |
||
| 47 | |||
| 48 | /** @see $schemaDataType */ |
||
| 49 | const SCHEMA_DATA_TYPE_HIDDEN = 'Hidden'; |
||
| 50 | |||
| 51 | /** @see $schemaDataType */ |
||
| 52 | const SCHEMA_DATA_TYPE_TEXT = 'Text'; |
||
| 53 | |||
| 54 | /** @see $schemaDataType */ |
||
| 55 | const SCHEMA_DATA_TYPE_HTML = 'HTML'; |
||
| 56 | |||
| 57 | /** @see $schemaDataType */ |
||
| 58 | const SCHEMA_DATA_TYPE_INTEGER = 'Integer'; |
||
| 59 | |||
| 60 | /** @see $schemaDataType */ |
||
| 61 | const SCHEMA_DATA_TYPE_DECIMAL = 'Decimal'; |
||
| 62 | |||
| 63 | /** @see $schemaDataType */ |
||
| 64 | const SCHEMA_DATA_TYPE_MULTISELECT = 'MultiSelect'; |
||
| 65 | |||
| 66 | /** @see $schemaDataType */ |
||
| 67 | const SCHEMA_DATA_TYPE_SINGLESELECT = 'SingleSelect'; |
||
| 68 | |||
| 69 | /** @see $schemaDataType */ |
||
| 70 | const SCHEMA_DATA_TYPE_DATE = 'Date'; |
||
| 71 | |||
| 72 | /** @see $schemaDataType */ |
||
| 73 | const SCHEMA_DATA_TYPE_DATETIME = 'Datetime'; |
||
| 74 | |||
| 75 | /** @see $schemaDataType */ |
||
| 76 | const SCHEMA_DATA_TYPE_TIME = 'Time'; |
||
| 77 | |||
| 78 | /** @see $schemaDataType */ |
||
| 79 | const SCHEMA_DATA_TYPE_BOOLEAN = 'Boolean'; |
||
| 80 | |||
| 81 | /** @see $schemaDataType */ |
||
| 82 | const SCHEMA_DATA_TYPE_CUSTOM = 'Custom'; |
||
| 83 | |||
| 84 | /** @see $schemaDataType */ |
||
| 85 | const SCHEMA_DATA_TYPE_STRUCTURAL = 'Structural'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var Form |
||
| 89 | */ |
||
| 90 | protected $form; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * This is INPUT's type attribute value. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $inputType = 'text'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $name; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var null|string |
||
| 106 | */ |
||
| 107 | protected $title; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var mixed |
||
| 111 | */ |
||
| 112 | protected $value; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected $extraClass; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Adds a title attribute to the markup. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | * |
||
| 124 | * @todo Implement in all subclasses |
||
| 125 | */ |
||
| 126 | protected $description; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Extra CSS classes for the FormField container. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $extraClasses; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @config |
||
| 137 | * @var array $default_classes The default classes to apply to the FormField |
||
| 138 | */ |
||
| 139 | private static $default_classes = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Right-aligned, contextual label for the field. |
||
| 143 | * |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $rightTitle; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Left-aligned, contextual label for the field. |
||
| 150 | * |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $leftTitle; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Stores a reference to the FieldList that contains this object. |
||
| 157 | * |
||
| 158 | * @var FieldList |
||
| 159 | */ |
||
| 160 | protected $containerFieldList; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var bool |
||
| 164 | */ |
||
| 165 | protected $readonly = false; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var bool |
||
| 169 | */ |
||
| 170 | protected $disabled = false; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var bool |
||
| 174 | */ |
||
| 175 | protected $autofocus = false; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Custom validation message for the field. |
||
| 179 | * |
||
| 180 | * @var string |
||
| 181 | */ |
||
| 182 | protected $customValidationMessage = ''; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Name of the template used to render this form field. If not set, then will look up the class |
||
| 186 | * ancestry for the first matching template where the template name equals the class name. |
||
| 187 | * |
||
| 188 | * To explicitly use a custom template or one named other than the form field see |
||
| 189 | * {@link setTemplate()}. |
||
| 190 | * |
||
| 191 | * @var string |
||
| 192 | */ |
||
| 193 | protected $template; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Name of the template used to render this form field. If not set, then will look up the class |
||
| 197 | * ancestry for the first matching template where the template name equals the class name. |
||
| 198 | * |
||
| 199 | * To explicitly use a custom template or one named other than the form field see |
||
| 200 | * {@link setFieldHolderTemplate()}. |
||
| 201 | * |
||
| 202 | * @var string |
||
| 203 | */ |
||
| 204 | protected $fieldHolderTemplate; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @var string |
||
| 208 | */ |
||
| 209 | protected $smallFieldHolderTemplate; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * All attributes on the form field (not the field holder). |
||
| 213 | * |
||
| 214 | * Partially determined based on other instance properties. |
||
| 215 | * |
||
| 216 | * @see getAttributes() |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | */ |
||
| 220 | protected $attributes = []; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * The data type backing the field. Represents the type of value the |
||
| 224 | * form expects to receive via a postback. Should be set in subclasses. |
||
| 225 | * |
||
| 226 | * The values allowed in this list include: |
||
| 227 | * |
||
| 228 | * - String: Single line text |
||
| 229 | * - Hidden: Hidden field which is posted back without modification |
||
| 230 | * - Text: Multi line text |
||
| 231 | * - HTML: Rich html text |
||
| 232 | * - Integer: Whole number value |
||
| 233 | * - Decimal: Decimal value |
||
| 234 | * - MultiSelect: Select many from source |
||
| 235 | * - SingleSelect: Select one from source |
||
| 236 | * - Date: Date only |
||
| 237 | * - DateTime: Date and time |
||
| 238 | * - Time: Time only |
||
| 239 | * - Boolean: Yes or no |
||
| 240 | * - Custom: Custom type declared by the front-end component. For fields with this type, |
||
| 241 | * the component property is mandatory, and will determine the posted value for this field. |
||
| 242 | * - Structural: Represents a field that is NOT posted back. This may contain other fields, |
||
| 243 | * or simply be a block of stand-alone content. As with 'Custom', |
||
| 244 | * the component property is mandatory if this is assigned. |
||
| 245 | * |
||
| 246 | * Each value has an equivalent constant, e.g. {@link self::SCHEMA_DATA_TYPE_STRING}. |
||
| 247 | * |
||
| 248 | * @var string |
||
| 249 | */ |
||
| 250 | protected $schemaDataType; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * The type of front-end component to render the FormField as. |
||
| 254 | * |
||
| 255 | * @skipUpgrade |
||
| 256 | * @var string |
||
| 257 | */ |
||
| 258 | protected $schemaComponent; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Structured schema data representing the FormField. |
||
| 262 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
| 263 | * |
||
| 264 | * @var array |
||
| 265 | */ |
||
| 266 | protected $schemaData = []; |
||
| 267 | |||
| 268 | private static $casting = array( |
||
|
|
|||
| 269 | 'FieldHolder' => 'HTMLFragment', |
||
| 270 | 'SmallFieldHolder' => 'HTMLFragment', |
||
| 271 | 'Field' => 'HTMLFragment', |
||
| 272 | 'AttributesHTML' => 'HTMLFragment', // property $AttributesHTML version |
||
| 273 | 'getAttributesHTML' => 'HTMLFragment', // method $getAttributesHTML($arg) version |
||
| 274 | 'Value' => 'Text', |
||
| 275 | 'extraClass' => 'Text', |
||
| 276 | 'ID' => 'Text', |
||
| 277 | 'isReadOnly' => 'Boolean', |
||
| 278 | 'HolderID' => 'Text', |
||
| 279 | 'Title' => 'Text', |
||
| 280 | 'RightTitle' => 'Text', |
||
| 281 | 'Description' => 'HTMLFragment', |
||
| 282 | ); |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Structured schema state representing the FormField's current data and validation. |
||
| 286 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
| 287 | * |
||
| 288 | * @var array |
||
| 289 | */ |
||
| 290 | protected $schemaState = []; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Takes a field name and converts camelcase to spaced words. Also resolves combined field |
||
| 294 | * names with dot syntax to spaced words. |
||
| 295 | * |
||
| 296 | * Examples: |
||
| 297 | * |
||
| 298 | * - 'TotalAmount' will return 'Total Amount' |
||
| 299 | * - 'Organisation.ZipCode' will return 'Organisation Zip Code' |
||
| 300 | * |
||
| 301 | * @param string $fieldName |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public static function name_to_label($fieldName) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Construct and return HTML tag. |
||
| 320 | * |
||
| 321 | * @param string $tag |
||
| 322 | * @param array $attributes |
||
| 323 | * @param null|string $content |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public static function create_tag($tag, $attributes, $content = null) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Creates a new field. |
||
| 375 | * |
||
| 376 | * @param string $name The internal field name, passed to forms. |
||
| 377 | * @param null|string $title The human-readable field label. |
||
| 378 | * @param mixed $value The value of the field. |
||
| 379 | */ |
||
| 380 | public function __construct($name, $title = null, $value = null) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Set up the default classes for the form. This is done on construct so that the default classes can be removed |
||
| 401 | * after instantiation |
||
| 402 | */ |
||
| 403 | protected function setupDefaultClasses() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return a link to this field. |
||
| 415 | * |
||
| 416 | * @param string $action |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function Link($action = null) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the HTML ID of the field. |
||
| 427 | * |
||
| 428 | * The ID is generated as FormName_FieldName. All Field functions should ensure that this ID is |
||
| 429 | * included in the field. |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function ID() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns the HTML ID for the form field holder element. |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function HolderID() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the current {@link FormTemplateHelper} on either the parent |
||
| 450 | * Form or the global helper set through the {@link Injector} layout. |
||
| 451 | * |
||
| 452 | * To customize a single {@link FormField}, use {@link setTemplate} and |
||
| 453 | * provide a custom template name. |
||
| 454 | * |
||
| 455 | * @return FormTemplateHelper |
||
| 456 | */ |
||
| 457 | public function getTemplateHelper() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns the field name. |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getName() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Returns the field input name. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getInputType() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the field value. |
||
| 488 | * |
||
| 489 | * @see FormField::setSubmittedValue() |
||
| 490 | * @return mixed |
||
| 491 | */ |
||
| 492 | public function Value() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Method to save this form field into the given {@link DataObject}. |
||
| 499 | * |
||
| 500 | * By default, makes use of $this->dataValue() |
||
| 501 | * |
||
| 502 | * @param DataObject|DataObjectInterface $record DataObject to save data into |
||
| 503 | */ |
||
| 504 | public function saveInto(DataObjectInterface $record) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns the field value suitable for insertion into the data object. |
||
| 513 | * @see Formfield::setValue() |
||
| 514 | * @return mixed |
||
| 515 | */ |
||
| 516 | public function dataValue() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns the field label - used by templates. |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function Title() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set the title of this formfield. |
||
| 533 | * Note: This expects escaped HTML. |
||
| 534 | * |
||
| 535 | * @param string $title Escaped HTML for title |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | public function setTitle($title) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Gets the contextual label than can be used for additional field description. |
||
| 546 | * Can be shown to the right or under the field in question. |
||
| 547 | * |
||
| 548 | * @return string Contextual label text. |
||
| 549 | */ |
||
| 550 | public function RightTitle() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Sets the right title for this formfield |
||
| 557 | * Note: This expects escaped HTML. |
||
| 558 | * |
||
| 559 | * @param string $rightTitle Escaped HTML for title |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function setRightTitle($rightTitle) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public function LeftTitle() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param string $leftTitle |
||
| 578 | * |
||
| 579 | * @return $this |
||
| 580 | */ |
||
| 581 | public function setLeftTitle($leftTitle) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Compiles all CSS-classes. Optionally includes a "form-group--no-label" class if no title was set on the |
||
| 590 | * FormField. |
||
| 591 | * |
||
| 592 | * Uses {@link Message()} and {@link MessageType()} to add validation error classes which can |
||
| 593 | * be used to style the contained tags. |
||
| 594 | * |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function extraClass() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Add one or more CSS-classes to the FormField container. |
||
| 627 | * |
||
| 628 | * Multiple class names should be space delimited. |
||
| 629 | * |
||
| 630 | * @param string $class |
||
| 631 | * |
||
| 632 | * @return $this |
||
| 633 | */ |
||
| 634 | View Code Duplication | public function addExtraClass($class) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Remove one or more CSS-classes from the FormField container. |
||
| 647 | * |
||
| 648 | * @param string $class |
||
| 649 | * |
||
| 650 | * @return $this |
||
| 651 | */ |
||
| 652 | View Code Duplication | public function removeExtraClass($class) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Set an HTML attribute on the field element, mostly an <input> tag. |
||
| 665 | * |
||
| 666 | * Some attributes are best set through more specialized methods, to avoid interfering with |
||
| 667 | * built-in behaviour: |
||
| 668 | * |
||
| 669 | * - 'class': {@link addExtraClass()} |
||
| 670 | * - 'title': {@link setDescription()} |
||
| 671 | * - 'value': {@link setValue} |
||
| 672 | * - 'name': {@link setName} |
||
| 673 | * |
||
| 674 | * Caution: this doesn't work on most fields which are composed of more than one HTML form |
||
| 675 | * field. |
||
| 676 | * |
||
| 677 | * @param string $name |
||
| 678 | * @param string $value |
||
| 679 | * |
||
| 680 | * @return $this |
||
| 681 | */ |
||
| 682 | public function setAttribute($name, $value) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get an HTML attribute defined by the field, or added through {@link setAttribute()}. |
||
| 691 | * |
||
| 692 | * Caution: this doesn't work on all fields, see {@link setAttribute()}. |
||
| 693 | * |
||
| 694 | * @param string $name |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | public function getAttribute($name) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Allows customization through an 'updateAttributes' hook on the base class. |
||
| 710 | * Existing attributes are passed in as the first argument and can be manipulated, |
||
| 711 | * but any attributes added through a subclass implementation won't be included. |
||
| 712 | * |
||
| 713 | * @return array |
||
| 714 | */ |
||
| 715 | public function getAttributes() |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Custom attributes to process. Falls back to {@link getAttributes()}. |
||
| 742 | * |
||
| 743 | * If at least one argument is passed as a string, all arguments act as excludes, by name. |
||
| 744 | * |
||
| 745 | * @param array $attributes |
||
| 746 | * |
||
| 747 | * @return string |
||
| 748 | */ |
||
| 749 | public function getAttributesHTML($attributes = null) |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Returns a version of a title suitable for insertion into an HTML attribute. |
||
| 790 | * |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | public function attrTitle() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Returns a version of a title suitable for insertion into an HTML attribute. |
||
| 800 | * |
||
| 801 | * @return string |
||
| 802 | */ |
||
| 803 | public function attrValue() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Set the field value. |
||
| 810 | * |
||
| 811 | * If a FormField requires specific behaviour for loading content from either the database |
||
| 812 | * or a submitted form value they should override setSubmittedValue() instead. |
||
| 813 | * |
||
| 814 | * @param mixed $value Either the parent object, or array of source data being loaded |
||
| 815 | * @param array|DataObject $data {@see Form::loadDataFrom} |
||
| 816 | * @return $this |
||
| 817 | */ |
||
| 818 | public function setValue($value, $data = null) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Set value assigned from a submitted form postback. |
||
| 826 | * Can be overridden to handle custom behaviour for user-localised |
||
| 827 | * data formats. |
||
| 828 | * |
||
| 829 | * @param mixed $value |
||
| 830 | * @param array|DataObject $data |
||
| 831 | * @return $this |
||
| 832 | */ |
||
| 833 | public function setSubmittedValue($value, $data = null) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Set the field name. |
||
| 840 | * |
||
| 841 | * @param string $name |
||
| 842 | * |
||
| 843 | * @return $this |
||
| 844 | */ |
||
| 845 | public function setName($name) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Set the field input type. |
||
| 854 | * |
||
| 855 | * @param string $type |
||
| 856 | * |
||
| 857 | * @return $this |
||
| 858 | */ |
||
| 859 | public function setInputType($type) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Set the container form. |
||
| 868 | * |
||
| 869 | * This is called automatically when fields are added to forms. |
||
| 870 | * |
||
| 871 | * @param Form $form |
||
| 872 | * |
||
| 873 | * @return $this |
||
| 874 | */ |
||
| 875 | public function setForm($form) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Get the currently used form. |
||
| 884 | * |
||
| 885 | * @return Form |
||
| 886 | */ |
||
| 887 | public function getForm() |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Return true if security token protection is enabled on the parent {@link Form}. |
||
| 894 | * |
||
| 895 | * @return bool |
||
| 896 | */ |
||
| 897 | public function securityTokenEnabled() |
||
| 907 | |||
| 908 | View Code Duplication | public function castingHelper($field) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Set the custom error message to show instead of the default format. |
||
| 919 | * |
||
| 920 | * Different from setError() as that appends it to the standard error messaging. |
||
| 921 | * |
||
| 922 | * @param string $customValidationMessage |
||
| 923 | * |
||
| 924 | * @return $this |
||
| 925 | */ |
||
| 926 | public function setCustomValidationMessage($customValidationMessage) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get the custom error message for this form field. If a custom message has not been defined |
||
| 935 | * then just return blank. The default error is defined on {@link Validator}. |
||
| 936 | * |
||
| 937 | * @return string |
||
| 938 | */ |
||
| 939 | public function getCustomValidationMessage() |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Set name of template (without path or extension). |
||
| 946 | * |
||
| 947 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
| 948 | * method on the subclass for support. |
||
| 949 | * |
||
| 950 | * @param string $template |
||
| 951 | * |
||
| 952 | * @return $this |
||
| 953 | */ |
||
| 954 | public function setTemplate($template) |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @return string |
||
| 963 | */ |
||
| 964 | public function getTemplate() |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @return string |
||
| 971 | */ |
||
| 972 | public function getFieldHolderTemplate() |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Set name of template (without path or extension) for the holder, which in turn is |
||
| 979 | * responsible for rendering {@link Field()}. |
||
| 980 | * |
||
| 981 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
| 982 | * method on the subclass for support. |
||
| 983 | * |
||
| 984 | * @param string $fieldHolderTemplate |
||
| 985 | * |
||
| 986 | * @return $this |
||
| 987 | */ |
||
| 988 | public function setFieldHolderTemplate($fieldHolderTemplate) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * @return string |
||
| 997 | */ |
||
| 998 | public function getSmallFieldHolderTemplate() |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Set name of template (without path or extension) for the small holder, which in turn is |
||
| 1005 | * responsible for rendering {@link Field()}. |
||
| 1006 | * |
||
| 1007 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
| 1008 | * method on the subclass for support. |
||
| 1009 | * |
||
| 1010 | * @param string $smallFieldHolderTemplate |
||
| 1011 | * |
||
| 1012 | * @return $this |
||
| 1013 | */ |
||
| 1014 | public function setSmallFieldHolderTemplate($smallFieldHolderTemplate) |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Returns the form field. |
||
| 1023 | * |
||
| 1024 | * Although FieldHolder is generally what is inserted into templates, all of the field holder |
||
| 1025 | * templates make use of $Field. It's expected that FieldHolder will give you the "complete" |
||
| 1026 | * representation of the field on the form, whereas Field will give you the core editing widget, |
||
| 1027 | * such as an input tag. |
||
| 1028 | * |
||
| 1029 | * @param array $properties |
||
| 1030 | * @return DBHTMLText |
||
| 1031 | */ |
||
| 1032 | public function Field($properties = array()) |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Returns a "field holder" for this field. |
||
| 1056 | * |
||
| 1057 | * Forms are constructed by concatenating a number of these field holders. |
||
| 1058 | * |
||
| 1059 | * The default field holder is a label and a form field inside a div. |
||
| 1060 | * |
||
| 1061 | * @see FieldHolder.ss |
||
| 1062 | * |
||
| 1063 | * @param array $properties |
||
| 1064 | * |
||
| 1065 | * @return DBHTMLText |
||
| 1066 | */ |
||
| 1067 | View Code Duplication | public function FieldHolder($properties = array()) |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Returns a restricted field holder used within things like FieldGroups. |
||
| 1080 | * |
||
| 1081 | * @param array $properties |
||
| 1082 | * |
||
| 1083 | * @return string |
||
| 1084 | */ |
||
| 1085 | View Code Duplication | public function SmallFieldHolder($properties = array()) |
|
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Returns an array of templates to use for rendering {@link FieldHolder}. |
||
| 1098 | * |
||
| 1099 | * @return array |
||
| 1100 | */ |
||
| 1101 | public function getTemplates() |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Returns an array of templates to use for rendering {@link FieldHolder}. |
||
| 1108 | * |
||
| 1109 | * @return array |
||
| 1110 | */ |
||
| 1111 | public function getFieldHolderTemplates() |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Returns an array of templates to use for rendering {@link SmallFieldHolder}. |
||
| 1121 | * |
||
| 1122 | * @return array |
||
| 1123 | */ |
||
| 1124 | public function getSmallFieldHolderTemplates() |
||
| 1131 | |||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Generate an array of class name strings to use for rendering this form field into HTML. |
||
| 1135 | * |
||
| 1136 | * @param string $customTemplate |
||
| 1137 | * @param string $customTemplateSuffix |
||
| 1138 | * |
||
| 1139 | * @return array |
||
| 1140 | */ |
||
| 1141 | protected function _templates($customTemplate = null, $customTemplateSuffix = null) |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Returns true if this field is a composite field. |
||
| 1154 | * |
||
| 1155 | * To create composite field types, you should subclass {@link CompositeField}. |
||
| 1156 | * |
||
| 1157 | * @return bool |
||
| 1158 | */ |
||
| 1159 | public function isComposite() |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Returns true if this field has its own data. |
||
| 1166 | * |
||
| 1167 | * Some fields, such as titles and composite fields, don't actually have any data. It doesn't |
||
| 1168 | * make sense for data-focused methods to look at them. By overloading hasData() to return |
||
| 1169 | * false, you can prevent any data-focused methods from looking at it. |
||
| 1170 | * |
||
| 1171 | * @see FieldList::collateDataFields() |
||
| 1172 | * |
||
| 1173 | * @return bool |
||
| 1174 | */ |
||
| 1175 | public function hasData() |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * @return bool |
||
| 1182 | */ |
||
| 1183 | public function isReadonly() |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Sets a read-only flag on this FormField. |
||
| 1190 | * |
||
| 1191 | * Use performReadonlyTransformation() to transform this instance. |
||
| 1192 | * |
||
| 1193 | * Setting this to false has no effect on the field. |
||
| 1194 | * |
||
| 1195 | * @param bool $readonly |
||
| 1196 | * |
||
| 1197 | * @return $this |
||
| 1198 | */ |
||
| 1199 | public function setReadonly($readonly) |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * @return bool |
||
| 1207 | */ |
||
| 1208 | public function isDisabled() |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Sets a disabled flag on this FormField. |
||
| 1215 | * |
||
| 1216 | * Use performDisabledTransformation() to transform this instance. |
||
| 1217 | * |
||
| 1218 | * Setting this to false has no effect on the field. |
||
| 1219 | * |
||
| 1220 | * @param bool $disabled |
||
| 1221 | * |
||
| 1222 | * @return $this |
||
| 1223 | */ |
||
| 1224 | public function setDisabled($disabled) |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * @return bool |
||
| 1233 | */ |
||
| 1234 | public function isAutofocus() |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Sets a autofocus flag on this FormField. |
||
| 1241 | * |
||
| 1242 | * @param bool $autofocus |
||
| 1243 | * @return $this |
||
| 1244 | */ |
||
| 1245 | public function setAutofocus($autofocus) |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Returns a read-only version of this field. |
||
| 1253 | * |
||
| 1254 | * @return FormField |
||
| 1255 | */ |
||
| 1256 | public function performReadonlyTransformation() |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Return a disabled version of this field. |
||
| 1273 | * |
||
| 1274 | * Tries to find a class of the class name of this field suffixed with "_Disabled", failing |
||
| 1275 | * that, finds a method {@link setDisabled()}. |
||
| 1276 | * |
||
| 1277 | * @return FormField |
||
| 1278 | */ |
||
| 1279 | public function performDisabledTransformation() |
||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * @param FormTransformation $transformation |
||
| 1296 | * |
||
| 1297 | * @return mixed |
||
| 1298 | */ |
||
| 1299 | public function transform(FormTransformation $transformation) |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * @param string $class |
||
| 1306 | * |
||
| 1307 | * @return int |
||
| 1308 | */ |
||
| 1309 | public function hasClass($class) |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * Returns the field type. |
||
| 1320 | * |
||
| 1321 | * The field type is the class name with the word Field dropped off the end, all lowercase. |
||
| 1322 | * |
||
| 1323 | * It's handy for assigning HTML classes. Doesn't signify the <input type> attribute. |
||
| 1324 | * |
||
| 1325 | * @see {link getAttributes()}. |
||
| 1326 | * |
||
| 1327 | * @return string |
||
| 1328 | */ |
||
| 1329 | public function Type() |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Abstract method each {@link FormField} subclass must implement, determines whether the field |
||
| 1337 | * is valid or not based on the value. |
||
| 1338 | * |
||
| 1339 | * @todo Make this abstract. |
||
| 1340 | * |
||
| 1341 | * @param Validator $validator |
||
| 1342 | * @return bool |
||
| 1343 | */ |
||
| 1344 | public function validate($validator) |
||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Describe this field, provide help text for it. |
||
| 1351 | * |
||
| 1352 | * By default, renders as a <span class="description"> underneath the form field. |
||
| 1353 | * |
||
| 1354 | * @param string $description |
||
| 1355 | * |
||
| 1356 | * @return $this |
||
| 1357 | */ |
||
| 1358 | public function setDescription($description) |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * @return string |
||
| 1367 | */ |
||
| 1368 | public function getDescription() |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * @return string |
||
| 1375 | */ |
||
| 1376 | public function debug() |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * This function is used by the template processor. If you refer to a field as a $ variable, it |
||
| 1390 | * will return the $Field value. |
||
| 1391 | * |
||
| 1392 | * @return string |
||
| 1393 | */ |
||
| 1394 | public function forTemplate() |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * @return bool |
||
| 1401 | */ |
||
| 1402 | public function Required() |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * Set the FieldList that contains this field. |
||
| 1413 | * |
||
| 1414 | * @param FieldList $containerFieldList |
||
| 1415 | * @return $this |
||
| 1416 | */ |
||
| 1417 | public function setContainerFieldList($containerFieldList) |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Get the FieldList that contains this field. |
||
| 1425 | * |
||
| 1426 | * @return FieldList |
||
| 1427 | */ |
||
| 1428 | public function getContainerFieldList() |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @return null|FieldList |
||
| 1435 | */ |
||
| 1436 | public function rootFieldList() |
||
| 1449 | |||
| 1450 | /** |
||
| 1451 | * Returns another instance of this field, but "cast" to a different class. The logic tries to |
||
| 1452 | * retain all of the instance properties, and may be overloaded by subclasses to set additional |
||
| 1453 | * ones. |
||
| 1454 | * |
||
| 1455 | * Assumes the standard FormField parameter signature with its name as the only mandatory |
||
| 1456 | * argument. Mainly geared towards creating *_Readonly or *_Disabled subclasses of the same |
||
| 1457 | * type, or casting to a {@link ReadonlyField}. |
||
| 1458 | * |
||
| 1459 | * Does not copy custom field templates, since they probably won't apply to the new instance. |
||
| 1460 | * |
||
| 1461 | * @param mixed $classOrCopy Class name for copy, or existing copy instance to update |
||
| 1462 | * |
||
| 1463 | * @return FormField |
||
| 1464 | */ |
||
| 1465 | public function castedCopy($classOrCopy) |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Determine if the value of this formfield accepts front-end submitted values and is saveable. |
||
| 1493 | * |
||
| 1494 | * @return bool |
||
| 1495 | */ |
||
| 1496 | public function canSubmitValue() |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * Sets the component type the FormField will be rendered as on the front-end. |
||
| 1503 | * |
||
| 1504 | * @param string $componentType |
||
| 1505 | * @return FormField |
||
| 1506 | */ |
||
| 1507 | public function setSchemaComponent($componentType) |
||
| 1512 | |||
| 1513 | /** |
||
| 1514 | * Gets the type of front-end component the FormField will be rendered as. |
||
| 1515 | * |
||
| 1516 | * @return string |
||
| 1517 | */ |
||
| 1518 | public function getSchemaComponent() |
||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Sets the schema data used for rendering the field on the front-end. |
||
| 1525 | * Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}. |
||
| 1526 | * Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored. |
||
| 1527 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
| 1528 | * |
||
| 1529 | * @param array $schemaData - The data to be merged with $this->schemaData. |
||
| 1530 | * @return FormField |
||
| 1531 | * |
||
| 1532 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
| 1533 | */ |
||
| 1534 | public function setSchemaData($schemaData = []) |
||
| 1540 | |||
| 1541 | /** |
||
| 1542 | * Gets the schema data used to render the FormField on the front-end. |
||
| 1543 | * |
||
| 1544 | * @return array |
||
| 1545 | */ |
||
| 1546 | public function getSchemaData() |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * @todo Throw exception if value is missing, once a form field schema is mandatory across the CMS |
||
| 1554 | * |
||
| 1555 | * @return string |
||
| 1556 | */ |
||
| 1557 | public function getSchemaDataType() |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Gets the defaults for $schemaData. |
||
| 1564 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaData()} are ignored. |
||
| 1565 | * Instead the `data` array should be used to pass around ad hoc data. |
||
| 1566 | * |
||
| 1567 | * @return array |
||
| 1568 | */ |
||
| 1569 | public function getSchemaDataDefaults() |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Sets the schema data used for rendering the field on the front-end. |
||
| 1596 | * Merges the passed array with the current `$schemaState` or {@link getSchemaStateDefaults()}. |
||
| 1597 | * Any passed keys that are not defined in {@link getSchemaStateDefaults()} are ignored. |
||
| 1598 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
| 1599 | * |
||
| 1600 | * @param array $schemaState The data to be merged with $this->schemaData. |
||
| 1601 | * @return FormField |
||
| 1602 | * |
||
| 1603 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
| 1604 | */ |
||
| 1605 | public function setSchemaState($schemaState = []) |
||
| 1611 | |||
| 1612 | /** |
||
| 1613 | * Gets the schema state used to render the FormField on the front-end. |
||
| 1614 | * |
||
| 1615 | * @return array |
||
| 1616 | */ |
||
| 1617 | public function getSchemaState() |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Gets the defaults for $schemaState. |
||
| 1625 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaState()} are ignored. |
||
| 1626 | * Instead the `data` array should be used to pass around ad hoc data. |
||
| 1627 | * Includes validation data if the field is associated to a {@link Form}, |
||
| 1628 | * and {@link Form->validate()} has been called. |
||
| 1629 | * |
||
| 1630 | * @todo Make form / field messages not always stored as html; Store value / casting as separate values. |
||
| 1631 | * @return array |
||
| 1632 | */ |
||
| 1633 | public function getSchemaStateDefaults() |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Return list of validation rules. Each rule is a key value pair. |
||
| 1648 | * The key is the rule name. The value is any information the frontend |
||
| 1649 | * validation handler can understand, or just `true` to enable. |
||
| 1650 | * |
||
| 1651 | * @return array |
||
| 1652 | */ |
||
| 1653 | public function getSchemaValidation() |
||
| 1660 | } |
||
| 1661 |