Form Component Custom Validator
Inherit from the class FormComponentCustomValidator and override the desired validation methods in order to implement a concrete validator for a Form Component.
Validation methods
ValidateFieldsOnSaveAsync: Use this method to validate the fields of the form before create and edit.ValidateDeleteAsync: Validate the delete operation of the main item of the page, or many items when the request comes from bulk edit page.
Dependency Injection
MIB supports dependency injection in concrete validators. Click here for more information.
Example
In this hypothetical validation, we override the validation methods ValidateFieldsOnSaveAsync and ValidateDeleteAsync. We use the inherited method AddNotification to add notifications to describe issues with the persistence data.
Key points
- Notifications will be aggregated and displayed to the user.
- Dictionary keys can be used in the messages.
public class MyConcreteFormComponentValidator : FormComponentCustomValidator
{
public override Task ValidateFieldsOnSaveAsync(IReadOnlyList<ReadOnlyFieldData> fields, FormComponentContext context, CancellationToken cancellationToken)
{
// Checking if the field `SALES_EMAIL` has a valid e-mail address.
var email = fields.GetValueAsString("SALES_EMAIL");
if (!EmailValidator.IsValid(email))
{
// Using the inherited method to add one or many notifications for your validation.
AddNotification(new Notification("Please provide a valid email"));
}
return Task.CompletedTask;
}
public override Task ValidateDeleteAsync(IReadOnlyList<int> mibObjectIds, FormComponentContext context, CancellationToken cancellationToken)
{
// Forbidding deletion of a specific object
int forbiddenId = 1000;
if(mibObjectIds.Contains(forbiddenId))
{
AddNotification(new Notification($"The item {forbiddenId} is protected against deletion"));
}
return Task.CompletedTask;
}
}