From c49f46f9e2500ff0944a603effe3050a4adade3b Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 11 Apr 2023 11:09:08 +0200 Subject: [PATCH] Add example for `FormFieldValidator` usage (#369) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- docs/php/api/form_builder/validation_data.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 -- 2.20.1