Form builder is only available since WoltLab Suite Core 5.2.
The migration guide for WoltLab Suite Core 5.2 provides some examples of how to migrate existing forms to form builder that can also help in understanding form builder if the old way of creating forms is familiar.
Advantages of Form Builder#
WoltLab Suite 5.2 introduces a new powerful way of creating forms: form builder.
Before taking a closer look at form builder, let us recap how forms are created in previous versions:
In general, for each form field, there is a corresponding property of the form's PHP class whose value has to be read from the request data, validated, and passed to the database object action to store the value in a database table.
When editing an object, the property's value has to be set using the value of the corresponding property of the edited object.
In the form's template, you have to write the
<form> element with all of its children: the
<section> elements, the
<dl> elements, and, of course, the form fields themselves.
In summary, this way of creating forms creates much duplicate or at least very similar code and makes it very time consuming if the structure of forms in general or a specific type of form field has to be changed.
Form builder, in contrast, relies on PHP objects representing each component of the form, from the form itself down to each form field. This approach makes creating forms as easy as creating some PHP objects, populating them with all the relevant data, and one line of code in the template to print the form.
Form Builder Components#
Form builder consists of several components that are presented on the following pages:
In general, form builder provides default implementation of interfaces by providing either abstract classes or traits. It is expected that the interfaces are always implemented using these abstract classes and traits! This way, if new methods are added to the interfaces, default implementations can be provided by the abstract classes and traits without causing backwards compatibility problems.
To make using form builder easier,
AbstractForm and provides most of the code needed to set up a form (of course without specific fields, those have to be added by the concrete form class), like reading and validating form values and using a database object action to use the form data to create or update a database object.
In addition to the existing methods inherited by
AbstractFormBuilderForm provides the following methods:
buildForm()builds the form in the following steps:
AbtractFormBuilderForm::createForm()to create the
IFormDocumentobject and add the form fields.
IFormDocument::build()to build the form.
AbtractFormBuilderForm::finalizeForm()to finalize the form like adding dependencies.
Additionally, between steps 1 and 2 and after step 3, the method provides two events,
buildForm to allow plugins to register event listeners to execute additional code at the right point in time.
createForm() creates the
FormDocument object and sets the form mode.
AbstractFormBuilderForm have to override this method (and call
parent::createForm() as the first line in the overridden method) to add concrete form containers and form fields to the bare form document.
finalizeForm() is called after the form has been built and the complete form hierarchy has been established.
This method should be overridden to add dependencies, for example.
setFormAction() is called at the end of
readData() and sets the form document’s action based on the controller class name and whether an object is currently edited.
- If an object is edited, at the beginning of
setFormObjectData() is called which calls
If values need to be loaded from additional sources, this method should be used for that.
AbstractFormBuilderForm also provides the following (public) properties:
IFormDocumentobject created in
editand handles which method of the database object is called by default (
createis called for
$formAction = 'create'and
updateis called for
$formAction = 'edit') and is used to set the value of the
IStorableObjectif the form is used to edit an existing object. For forms used to create objects,
null. Edit forms have to manually identify the edited object based on the request data and set the value of
$objectActionNamecan be used to set an alternative action to be executed by the database object action that deviates from the default action determined by the value of
$objectActionClassis the name of the database object action class that is used to create or update the database object.
Form builder forms can also be used in dialogs.
For such forms,
DialogFormDocument should be used which provides the additional methods
cancelable($cancelable = true) and
isCancelable() to set and check if the dialog can be canceled.
If a dialog form can be canceled, a cancel button is added.
If the dialog form is fetched via an AJAX request,
IFormDocument::ajax() has to be called.
AJAX forms are registered with
WoltLabSuite/Core/Form/Builder/Manager which also supports getting all of the data of a form via the
getData() function relies on all form fields creating and registering a
WoltLabSuite/Core/Form/Builder/Field/Field object that provides the data of a specific field.
To make it as easy as possible to work with AJAX forms in dialogs,
WoltLabSuite/Core/Form/Builder/Dialog (abbreviated as
FormBuilderDialog from now on) should generally be used instead of
The constructor of
FormBuilderDialog expects the following parameters:
dialogId: id of the dialog element
className: PHP class used to get the form dialog (and save the data if
actionName: name of the action/method of
classNamethat returns the dialog; the method is expected to return an array with
formIdcontaing the id of the returned form and
dialogcontaining the rendered form dialog
options: additional options:
actionParameters(default: empty): additional parameters sent during AJAX requests
true, whenever the dialog is closed, the form is destroyed so that a new form is fetched if the dialog is opened again
dialog: additional dialog options used as
optionsduring dialog setup
onSubmit: callback when the form is submitted (takes precedence over
submitActionName(default: not set): name of the action/method of
classNamecalled when the form is submitted
The three public functions of
destroy()destroys the dialog, the form, and all of the form fields.
getData()returns a Promise that returns the form data.
open()opens the dialog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18