WoltLab Suite 6.2 Handbuch
Deutsch/PHP-Version: Die Website-Struktur, Navigation und Überschriften sind auf Deutsch vorbereitet. Code-Beispiele und technische Namen bleiben unverändert.

Structure of Form Builder

Forms built with form builder consist of three major structural elements listed from top to bottom:

1. form document,

1. form container,

1. form field.

The basis for all three elements are form nodes.

!!! info "The form builder API uses fluent interfaces heavily, meaning that unless a method is a getter, it generally returns the objects itself to support method chaining."

Form Nodes

IFormNode

IFormNode is the base interface that any node of a form has to implement and it requires the following methods:

The following aspects are important when working with availability:

Availability sets the static availability for form nodes that does not change during the lifetime of a form.

In contrast, dependencies represent a dynamic availability for form nodes that depends on the current value of certain form fields.

TFormNode provides a default implementation of most of these methods.

IFormChildNode

IFormChildNode extends IFormNode for such elements of a form that can be a child node to a parent node and it requires the parent(IFormParentNode $parentNode) and getParent() methods used to set and get the node’s parent node.

TFormChildNode provides a default implementation of these two methods and also of IFormNode::getDocument().

IFormParentNode

IFormParentNode extends IFormNode for such elements of a form that can be a parent to child nodes.

Additionally, the interface also extends \Countable and \RecursiveIterator.

The interface requires the following methods:

IFormElement

IFormElement extends IFormNode for such elements of a form that can have a description and a label and it requires the following methods:

IObjectTypeFormNode

IObjectTypeFormField has to be implemented by form nodes that rely on a object type of a specific object type definition in order to function.

The implementing class has to implement the methods objectType($objectType), getObjectType(), and getObjectTypeDefinition().

TObjectTypeFormNode provides a default implementation of these three methods.

CustomFormNode

CustomFormNode is a form node whose contents can be set directly via content($content).

!!! warning "This class should generally not be relied on. Instead, TemplateFormNode should be used."

Example:

CustomFormNode::create('example')
  ->content('html code')

TemplateFormNode

TemplateFormNode is a form node whose contents are read from a template.

TemplateFormNode has the following additional methods:

Example:

TemplateFormNode::create('example')
  ->templateName('example_template_name')

Form Document

A form document object represents the form as a whole and has to implement the IFormDocument interface.

WoltLab Suite provides a default implementation with the FormDocument class.

IFormDocument should not be implemented directly but instead FormDocument should be extended to avoid issues if the IFormDocument interface changes in the future.

IFormDocument extends IFormParentNode and requires the following additional methods:

The last aspect is relevant for DialogFormDocument objects.

DialogFormDocument is a specialized class for forms in dialogs that, in contrast to FormDocument do not require an action to be set.

Additionally, DialogFormDocument provides the cancelable($cancelable = true) and isCancelable() methods used to determine if the dialog from can be canceled.

By default, dialog forms are cancelable.

Form Button

A form button object represents a button shown at the end of the form that, for example, submits the form.

Every form button has to implement the IFormButton interface that extends IFormChildNode and IFormElement.

IFormButton requires four methods to be implemented:

Form Container

A form container object represents a container for other form containers or form field directly.

Every form container has to implement the IFormContainer interface which requires the following method:

There are multiple default container implementations:

1. FormContainer is the default implementation of IFormContainer.

1. TabMenuFormContainer represents the container of tab menu, while

1. TabFormContainer represents a tab of a tab menu and

1. TabTabMenuFormContainer represents a tab of a tab menu that itself contains a tab menu.

1. The children of RowFormContainer are shown in a row and should use col-* classes.

1. The children of RowFormFieldContainer are also shown in a row but does not show the labels and descriptions of the individual form fields.

Instead of the individual labels and descriptions, the container's label and description is shown and both span all of fields.

1. SuffixFormFieldContainer can be used for one form field with a second selection form field used as a suffix.

The methods of the interfaces that FormContainer is implementing are well documented, but here is a short overview of the most important methods when setting up a form or extending a form with an event listener:

Example:

$form->appendChild(
  FormContainer::create('example')
    ->appendChildren([ ... ])
  );

Form Field

A form field object represents a concrete form field that allows entering data.

Every form field has to implement the IFormField interface which extends IFormChildNode and IFormElement.

IFormField requires the following additional methods:

The object property allows having different fields (requiring different ids) that represent the same object property which is handy when available options of the field’s value depend on another field.

Having object property allows to define different fields for each value of the other field and to use form field dependencies to only show the appropriate field.

AbstractFormField provides default implementations of many of the listed methods above and should be extended instead of implementing IFormField directly.

An overview of the form fields provided by default can be found here.

Form Field Interfaces and Traits

WoltLab Suite Core provides a variety of interfaces and matching traits with default implementations for several common features of form fields:

IAttributeFormField

IAttributeFormField has to be implemented by form fields for which attributes can be added to the actual form element (in addition to adding attributes to the surrounding element via the attribute-related methods of IFormNode).

The implementing class has to implement the methods fieldAttribute(string $name, string $value = null): self and getFieldAttribute(string $name): self/getFieldAttributes(): array, which are used to add and get the attributes, respectively.

Additionally, hasFieldAttribute(string $name): bool has to implemented to check if a certain attribute is present, removeFieldAttribute(string $name): self to remove an attribute, and static validateFieldAttribute(string $name) to check if the attribute is valid for this specific class.

TAttributeFormField provides a default implementation of these methods and TInputAttributeFormField specializes the trait for input-based form fields.

These two traits also ensure that if a specific interface that handles a specific attribute is implemented, like IAutoCompleteFormField handling autocomplete, this attribute cannot be set with this API.

Instead, the dedicated API provided by the relevant interface has to be used.

IAutoCompleteFormField

IAutoCompleteFormField has to be implemented by form fields that support the autocomplete attribute.

The implementing class has to implement the methods autoComplete(?string $autoComplete): self and getAutoComplete(): ?string, which are used to set and get the autocomplete value, respectively.

TAutoCompleteFormField provides a default implementation of these two methods and TTextAutoCompleteFormField specializes the trait for text form fields.

When using TAutoCompleteFormField, you have to implement the getValidAutoCompleteTokens(): array method which returns all valid autocomplete tokens.

IAutoFocusFormField

IAutoFocusFormField has to be implemented by form fields that can be auto-focused.

The implementing class has to implement the methods autoFocus($autoFocus = true) and isAutoFocused().

By default, form fields are not auto-focused.

TAutoFocusFormField provides a default implementation of these two methods.

ICensorshipFormField

ICensorshipFormField has to be implemented by form fields that support the censorship function.

The implementing class has to implement the methods censorship(bool $censorship = true): static, hasCensorship(): bool, and validateCensorship(string $text): void.

TCensorshipFormField provides a default implementation of these methods.

!!! warning "The implementing class has to validate the entered value manually by calling validateCensorship()."

ICssClassFormField

ICssClassFormField has to be implemented by form fields for which CSS classes can be added to the actual form element (in addition to adding CSS classes to the surrounding element via the class-related methods of IFormNode).

The implementing class has to implement the methods addFieldClass(string $class): self/addFieldClasses(array $classes): self and getFieldClasses(): array, which are used to add and get the CSS classes, respectively.

Additionally, hasFieldClass(string $class): bool has to implemented to check if a certain CSS class is present and removeFieldClass(string $class): self to remove a CSS class.

TCssClassFormField provides a default implementation of these methods.

IFileFormField

IFileFormField has to be implemented by every form field that uploads files so that the enctype attribute of the form document is multipart/form-data (see IFormDocument::getEnctype()).

IFilterableSelectionFormField

IFilterableSelectionFormField extends ISelectionFormField by the possibilty for users when selecting the value(s) to filter the list of available options.

The implementing class has to implement the methods filterable($filterable = true) and isFilterable().

TFilterableSelectionFormField provides a default implementation of these two methods.

II18nFormField

II18nFormField has to be implemented by form fields if the form field value can be entered separately for all available languages.

The implementing class has to implement the following methods:

TI18nFormField provides a default implementation of these eight methods and additional default implementations of some of the IFormField methods.

If multilingual input is enabled for a specific form field, classes using TI18nFormField register a custom form field data processor to add the array with multilingual input into the $parameters array directly using {$objectProperty}_i18n as the array key.

If multilingual input is enabled but only a monolingual value is entered, the custom form field data processor does nothing and the form field’s value is added by the DefaultFormDataProcessor into the data sub-array of the $parameters array.

!!! info "TI18nFormField already provides a default implementation of IFormField::validate()."

IImmutableFormField

IImmutableFormField has to be implemented by form fields that support being displayed but whose value cannot be changed.

The implementing class has to implement the methods immutable($immutable = true) and isImmutable() that can be used to determine if the value of the form field is mutable or immutable.

By default, form field are mutable.

IInputModeFormField

IInputModeFormField has to be implemented by form fields that support the inputmode attribute.

The implementing class has to implement the methods inputMode(?string $inputMode): self and getInputMode(): ?string, which are used to set and get the input mode, respectively.

TInputModeFormField provides a default implementation of these two methods.

IMaximumFormField

IMaximumFormField has to be implemented by form fields if the entered value must have a maximum value.

The implementing class has to implement the methods maximum($maximum = null) and getMaximum().

A maximum of null signals that no maximum value has been set.

TMaximumFormField provides a default implementation of these two methods.

!!! warning "The implementing class has to validate the entered value against the maximum value manually."

IMaximumLengthFormField

IMaximumLengthFormField has to be implemented by form fields if the entered value must have a maximum length.

The implementing class has to implement the methods maximumLength($maximumLength = null), getMaximumLength(), and validateMaximumLength($text, Language $language = null).

A maximum length of null signals that no maximum length has been set.

TMaximumLengthFormField provides a default implementation of these three methods.

!!! warning "The implementing class has to validate the entered value against the maximum value manually by calling validateMaximumLength()."

IMinimumFormField

IMinimumFormField has to be implemented by form fields if the entered value must have a minimum value.

The implementing class has to implement the methods minimum($minimum = null) and getMinimum().

A minimum of null signals that no minimum value has been set.

TMinimumFormField provides a default implementation of these three methods.

!!! warning "The implementing class has to validate the entered value against the minimum value manually."

IMinimumLengthFormField

IMinimumLengthFormField has to be implemented by form fields if the entered value must have a minimum length.

The implementing class has to implement the methods minimumLength($minimumLength = null), getMinimumLength(), and validateMinimumLength($text, Language $language = null).

A minimum length of null signals that no minimum length has been set.

TMinimumLengthFormField provides a default implementation of these three methods.

!!! warning "The implementing class has to validate the entered value against the minimum value manually by calling validateMinimumLength()."

IMultipleFormField

IMinimumLengthFormField has to be implemented by form fields that support selecting or setting multiple values.

The implementing class has to implement the following methods:

TMultipleFormField provides a default implementation of these six methods and classes using TMultipleFormField register a custom form field data processor to add the HtmlInputProcessor object with the text into the $parameters array directly using {$objectProperty}_htmlInputProcessor as the array key.

!!! warning "The implementing class has to validate the values against the minimum and maximum number of values manually."

INullableFormField

INullableFormField has to be implemented by form fields that support null as their (empty) value.

The implementing class has to implement the methods nullable($nullable = true) and isNullable().

TNullableFormField provides a default implementation of these two methods.

null should be returned by IFormField::getSaveValue() is the field is considered empty and the form field has been set as nullable.

IPaketeFormField

IPackagesFormField has to be implemented by form fields that, in some way, considers packages whose ids may be passed to the field object.

The implementing class has to implement the methods packageIDs(array $packageIDs) and getPackageIDs().

TPackagesFormField provides a default implementation of these two methods.

IPatternFormField

IPatternFormField has to be implemented by form fields that support the pattern attribute.

The implementing class has to implement the methods pattern(?string $pattern): self and getPattern(): ?string, which are used to set and get the pattern, respectively.

TPatternFormField provides a default implementation of these two methods.

IPlaceholderFormField

IPlaceholderFormField has to be implemented by form fields that support a placeholder value for empty fields.

The implementing class has to implement the methods placeholder($languageItem = null, array $variables = []) and getPlaceholder().

TPlaceholderFormField provides a default implementation of these two methods.

ISelectionFormField

ISelectionFormField has to be implemented by form fields with a predefined set of possible values.

The implementing class has to implement the getter and setter methods options($options, $nestedOptions = false, $labelLanguageItems = true) and getOptions() and additionally two methods related to nesting, i.e. whether the selectable options have a hierarchy:

supportsNestedOptions() and getNestedOptions().

TSelectionFormField provides a default implementation of these four methods.

ISuffixedFormField

ISuffixedFormField has to be implemented by form fields that support supports displaying a suffix behind the actual input field.

The implementing class has to implement the methods suffix($languageItem = null, array $variables = []) and getSuffix().

TSuffixedFormField provides a default implementation of these two methods.

TDefaultIdFormField

Form fields that have a default id have to use TDefaultIdFormField and have to implement the method getDefaultId().

Displaying Forms

The only thing to do in a template to display the whole form including all of the necessary JavaScript is to put

{unsafe:$form->getHtml()}

into the template file at the relevant position.