Validators - smbc-digital/form-builder GitHub Wiki

Validators

Overview

A validator is used to determine whether an elements value meets a specified criteria. For example EmailElementValidator will check the value is a correct email format, and also that it's not null or empty (unless it's optional).

All validators are run on every page submission, so you need to be explicit in the validator if any element types are to be ignored.

Creating a new validator

1. Create the Validator

Add a new .cs file inside the Validators folder, inheriting from IElementValidator. Implement the Validate method as required remembering to return true for any element types or circumstances which are not applicable to be checked.

2. Register the Validator in the ServiceCollectionExtension file

Because all the Validators are run automatically, all you need to do is register the Validator in the AddValidators method.