Add example for `FormFieldValidator` usage (#369)
authorMarcel Werk <burntime@woltlab.com>
Tue, 11 Apr 2023 09:09:08 +0000 (11:09 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Apr 2023 09:09:08 +0000 (11:09 +0200)
Co-authored-by: Tim Düsterhus <duesterhus@woltlab.com>
docs/php/api/form_builder/validation_data.md

index 8956f2bc12848aa58530dfd8cc6f8a07cb726bfe..ac4f2e84a98fcf33b4a6dae883708825bac3162b 100644 (file)
@@ -38,6 +38,26 @@ Form field validation errors are added to form fields via the `IFormField::addVa
 
 Form field validators are added to form fields via the `addValidator(IFormFieldValidator $validator)` method.
 
+#### Example
+
+The following source code adds a validator that validates whether the value in the input field matches a specific value.
+
+```php
+$container->appendChildren([
+       FooField::create('a')
+               ->addValidator(new FormFieldValidator('b', function (FooField $formField) {
+                       if ($formField->getValue() != 'value') {
+                               $formField->addValidationError(
+                                       new FormFieldValidationError(
+                                               'type',
+                                               'phrase'
+                                       )
+                               );
+                       }
+               })),
+       ]);
+```
+
 
 ## Form Data