ICustomFormValidator

The ICustomFormValidator interface allows you to write your own form validator, much like the ICustomFormFieldValidator, except it's not meant for single fields but the entire form.  ICustomFormValidators are applied to the form component under the Validation -> Custom Validators section.

You only need to implement the Validate() method which gets all the formValues as an argument.  If your validation succeeds, just return an empty string, otherwise return the validation/error message as a string.

Interface declaration

public interface ICustomFormValidator
{
	string Validate(Dictionary<string, object> formValues);
}

Example

In this example, we implement a simple form validator which can be applied to a form component.  It allows the user to check "Prevent Submission" which will cause the validation to fail - effectively preventing the submission of the form completely.  The user can also specify a message which will be shown to the user.

	[DisplayName("Prevent form submission")]
	[Description("Allows you to temporarily prevent submission of a form and to specify a message for the user")]
	public class PreventFormSubmissionFormValidator : ICustomFormValidator
	{
		[EditorBrowsable]
		public bool PreventSubmission { get; set; }

		[EditorBrowsable]
		public string Message { get; set; }

		public string Validate(Dictionary<string, object> formValues)
		{
			if (PreventSubmission)
			{
				return Message;			
			}

			return "";
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	services.AddNamed<ICustomFormValidator, PreventFormSubmissionFormValidator>(ServiceLifetime.Transient);
}