Add general example for form builder usage
authorMarcel Werk <burntime@woltlab.com>
Thu, 25 Apr 2024 13:13:21 +0000 (15:13 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 25 Apr 2024 13:13:21 +0000 (15:13 +0200)
See #421

docs/php/api/form_builder/overview.md

index 7f21e1c51caf68cd39317d7db19920f5c18c0c78..5e45227032d2d06508c704c2a78a15dc6cd2e93b 100644 (file)
@@ -48,6 +48,54 @@ In addition to the existing methods inherited by `AbstractForm`, `AbstractFormBu
 - `$objectActionName` can 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 `$formAction`.
 - `$objectActionClass` is the name of the database object action class that is used to create or update the database object.
 
+Example:
+
+```php
+<?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)
+        ]);
+    }
+}
+```
 
 ## `DialogFormDocument`