Validator
Description
The Validator is a utility component designed to orchestrate and centralize the validation of multiple UI elements. It registers components that implement validation, tracks which of them the user has interacted with, and triggers visual state updates based on their current validity. Use this component when you need to validate forms or groups of input components in a fluent and efficient manner.
Usage
Instantiate the Validator using the static helper from Tesserae.UI. Register your UI components (such as TextBox or Dropdown) with custom validation logic. This allows you to show validation messages dynamically as users interact with the form or when the entire form is revalidated (for instance, on form submission).
Below is a simple example demonstrating its usage:
API reference
public sealed class ValidatorA Validator class that coordinates validation for multiple components.
Constructors
Properties
Methods
public delegate void OnValidationHandler(ValidationState validity)The haveEncounteredInvalidValue value indicates whether any invalid values have been encountered SO FAR - components will only be validated as a User edits them OR when a Revalidate call is made (or the IsValid property is checked), which indicates an action such a form submission is about to occur and that EVERYTHING should be checked (unless such an action has occurred, we want to give Users a chance to fill things in BEFORE we shout at them about it)
public void Register<T>(ICanValidate<T> component, Func<bool> wouldBeValid, Action validate) where T : ICanValidate<T>Registers a component with the validator.
Type Parameters
- T
- The type of the component.
Parameters
- component
- The component to register.
- wouldBeValid
- A function that returns whether the component would be valid without updating its visual state.
- validate
- An action that performs validation and updates the component's visual state.
public void ResetState()Resets the validation state of all registered components.
public void RegisterFromCallback(Func<bool> isInvalid, Action onRevalidation)Registers a custom validation logic not tied to a specific component.
Parameters
- isInvalid
- A function that returns whether the state is invalid.
- onRevalidation
- An action to perform on revalidation.
public Validator OnValidation(OnValidationHandler onValidation)Adds a validation event handler.
Parameters
- onValidation
- The validation event handler.
Returns
The current instance of the type.
public Validator Debounce(int delayInMs)Sets the debounce delay for validation. The milliseconds must be a value of at least one, trying to disable Debounce by passing a zero (or negative) value is not supported.
Parameters
- delayInMs
- The delay in milliseconds.
Returns
The current instance of the type.
public Validator Debounce(int delayInMs, int maxDelayInMs)Sets the debounce delay and maximum delay for validation.
Parameters
- delayInMs
- The delay in milliseconds.
- maxDelayInMs
- The maximum delay in milliseconds.
Returns
The current instance of the type.
public bool AreCurrentValuesAllValid()This will check whether the form's values would currently be considered valid but without updating any the visual states relating to validity - this may be used when a form is being displayed to the User where the fields MIGHT all have been pre-populated and so the form may be valid already (but if it's not valid yet then we don't want the fields that the User hasn't edited yet to be shown as invalid until they've had a chance to interact with them). This would be used if the submit on the form should be set to disabled initially if the form is invalid (or enabled if IS valid) and subsequently updated on each ValidationOccured event.
public bool Revalidate()Triggers validation for all registered components and returns whether they are all valid.
Returns
True if all components are valid, false otherwise.
private ValidationState GetValidity(bool validateOnlyUserEditedComponents, bool updateComponentAppearances)This will return false if any of the components that were checked were found to be in an invalid state (the components checked depends upon validateOnlyUserEditedComponents and which registered components that the User has interacted with)
Samples
Basic Form Validation Example
The following sample demonstrates how to set up a simple form with two text boxes. Each text box has its own validation logic, and a validator is used to update the UI based on the current validity of the form.
See also
- TextBox: Often used in forms alongside Validator.
- Dropdown: Another input component that can be validated.