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.

Form Builder

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.

!!! info "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 Komponenten

Form builder consists of several components that are presented on the following pages:

1. Structure of form builder

1. Form validation and form data

1. Form node dependencies

!!! warning "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."

AbstractFormBuilderForm

To make using form builder easier, AbstractFormBuilderForm extends 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 AbstractForm, AbstractFormBuilderForm provides the following methods:

1. Call AbtractFormBuilderForm::createForm() to create the IFormDocument object and add the form fields.

2. Call IFormDocument::build() to build the form.

3. Call AbtractFormBuilderForm::finalizeForm() to finalize the form like adding dependencies.

Additionally, between steps 1 and 2 and after step 3, the method provides two events, createForm and buildForm to allow plugins to register event listeners to execute additional code at the right point in time.

AbstractFormBuilderForm also provides the following (public) properties:

Example:

<?php

namespace wcf\acp\form;

use wcf\form\AbstractFormBuilderForm;
use wcf\system\form\builder\field\BooleanFormField;
use wcf\system\form\builder\field\TextFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;

class FooAddForm extends AbstractFormBuilderForm
{
    /**
     * @inheritDoc
     */
    public $objectActionClass = FooAction::class;

    #[\Override]
    protected function createForm()
    {
        parent::createForm();

        $this->form->appendChildren([
            TextFormField::create('name')
                ->label('wcf.foo.name')
                ->description('wcf.foo.name.description')
                ->required()
                ->maximumLength(255)
                ->addValidator(new FormFieldValidator('notFoo', function (TextFormField $formField) {
                    if ($formField->getValue() === 'foo') {
                        $formField->addValidationError(
                            new FormFieldValidationError(
                                'isFoo',
                                'wcf.foo.name.error.isFoo'
                            )
                        );
                    }
                })),
            BooleanFormField::create('isCool')
                ->label('wcf.foo.isCool')
                ->value(true)
        ]);
    }
}

Psr15DialogForm

Form builder forms can also be used in dialogs. For such forms, Psr15DialogForm should be used which provides the additional methods validateRequest(ServerRequestInterface $request) and toResponse() to enable processing of the form via an AJAX request.

Example:

<?php

namespace wcf\action;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\form\builder\Psr15DialogForm;
use wcf\system\WCF;

final class FooAction implements RequestHandlerInterface
{
    /**
     * @inheritDoc
     */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $form = $this->getForm();

        if ($request->getMethod() === 'GET') {
            return $form->toResponse();
        } elseif ($request->getMethod() === 'POST') {
            $response = $form->validateRequest($request);
            if ($response !== null) {
                return $response;
            }

            $data = $form->getData()['data'];

            // process data
        } else {
            throw new \LogicException('Unreachable');
        }
    }

    private function getForm(): Psr15DialogForm
    {
        $form = new Psr15DialogForm(
            static::class,
            WCF::getLanguage()->get('wcf.foo.dialog.name')
        );
        $form->appendChildren([
            TextFormField::create('name')
                ->label('wcf.foo.name')
                ->description('wcf.foo.name.description')
                ->required()
                ->maximumLength(255)
                ->addValidator(new FormFieldValidator('notFoo', function (TextFormField $formField) {
                    if ($formField->getValue() === 'foo') {
                        $formField->addValidationError(
                            new FormFieldValidationError(
                                'isFoo',
                                'wcf.foo.name.error.isFoo'
                            )
                        );
                    }
                })),
            BooleanFormField::create('isCool')
                ->label('wcf.foo.isCool')
                ->value(true)
        ]);
        $form->build();

        return $form;
    }
}

On the client side, the dialog is loaded using the dialog API. A tuple is made available as a return, which provides the status of the successful form submission (ok: boolean) and the server-side return (result: any).

Example:

require(['WoltLabSuite/Core/Component/Dialog'], async ({ dialogFactory }) => {
	const { ok, result } = await dialogFactory().usingFormBuilder().fromEndpoint('endpoint_url');

	if (ok) {
		// Form submission was successful
	}
});