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.
- Namespace
- Tesserae
Constructors
| Name | Description |
|---|---|
| Validator | Initializes a new instance of the Validator class. |
Properties
| Name | Description |
|---|---|
| IsValid | This will trigger the validation logic for EVERY registered component and return false if any of them are not in a valid state (and, by doing so, their display state will be updated accordingly) |
Methods
| Name | Description |
|---|---|
| OnValidationHandler | 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) |
| Register<T> | Registers a component with the validator. |
| ResetState | Resets the validation state of all registered components. |
| RegisterFromCallback | Registers a custom validation logic not tied to a specific component. |
| OnValidation | Adds a validation event handler. |
| Debounce | 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. |
| 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. |
| Revalidate | Triggers validation for all registered components and returns whether they are all valid. |
| GetValidity | 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) |
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.
| Overload | |
|---|---|
| Debounce(int) | 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. |
| Debounce(int, int) | Sets the debounce delay and maximum delay for validation. |
Debounce(int)
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 int
- The 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)
Form
A real <form> container. Wrapping login inputs in one (with Action and proper autocomplete hints on the inputs) is what lets browser and third-party password managers detect the submission, scope saved credentials to the current origin rather than falling back to registrable-domain heuristics, and prompt the user to save or update the password.
public class Form : IContainer<Form, IComponent>, IHasMarginPaddingA <form> container. Wrapping login inputs in a real form (with Action and proper autocomplete hints on the inputs) is what lets browser and third-party password managers detect the submission, scope saved credentials to the current origin (subdomain included) instead of falling back to registrable-domain heuristics, and prompt the user to save or update the password.
- Namespace
- Tesserae
- Implements
- IContainer<Form, IComponent>, IHasMarginPadding
Properties
| Name | Description |
|---|---|
| Action | The form's submission URL. Should be a same-origin URL (absolute or relative) on the current subdomain — this is the strongest hint a password manager has for scoping the saved credential to location.host rather than to the registrable domain. |
| Method | The form's HTTP method (defaults to post). |
| FormName | The form's name attribute. |
| AutoComplete | The form-level autocomplete attribute (on / off). Leave unset to inherit the browser default. Setting this to off disables saving for the whole form, which is the opposite of what a sign-in form usually wants. |
public string Action { get ; set ; }The form's submission URL. Should be a same-origin URL (absolute or relative) on the current subdomain — this is the strongest hint a password manager has for scoping the saved credential to location.host rather than to the registrable domain.
Methods
| Name | Description |
|---|---|
| OnSubmit | Attaches a handler invoked when the form is submitted (via Enter on a child input or a <button type="submit"> inside the form). The native browser navigation is always cancelled. |
| Submit | Programmatically request submission (mirrors pressing Enter inside the form). Uses HTMLFormElement.requestSubmit() when available so that validation runs and the submit event fires; otherwise falls back to dispatching a synthetic event. |
public Form OnSubmit(ComponentEventHandler<Form, Event> onSubmit)Attaches a handler invoked when the form is submitted (via Enter on a child input or a <button type="submit"> inside the form). The native browser navigation is always cancelled.
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.