WoltLab Suite includes a powerful way of creating forms: Form Builder. Form builder allows you to easily define all the fields and their constraints and interdependencies within PHP with full IDE support. It will then automatically generate the necessary HTML with full interactivity to render all the fields and also validate the fields’ contents upon submission.
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.
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