From: Marcel Werk Date: Tue, 11 Apr 2023 09:09:08 +0000 (+0200) Subject: Add example for `FormFieldValidator` usage (#369) X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c49f46f9;p=GitHub%2FWoltLab%2Fwoltlab.github.io.git Add example for `FormFieldValidator` usage (#369) Co-authored-by: Tim Düsterhus --- diff --git a/docs/php/api/form_builder/validation_data.md b/docs/php/api/form_builder/validation_data.md index 8956f2bc..ac4f2e84 100644 --- a/docs/php/api/form_builder/validation_data.md +++ b/docs/php/api/form_builder/validation_data.md @@ -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