Language

EditContext.RegisterAsyncFieldValidator Method

Definition

Registers an asynchronous validation for a specific field. The validator is invoked immediately with a CancellationToken owned by this EditContext, and the returned Task is tracked for IsValidationPending(FieldIdentifier) and IsValidationFaulted(FieldIdentifier). A validation already tracked for the field is cancelled and replaced.

public void RegisterAsyncFieldValidator(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier, Func<System.Threading.CancellationToken,System.Threading.Tasks.Task> validator);
member this.RegisterAsyncFieldValidator : FieldIdentifier * Func<System.Threading.CancellationToken, System.Threading.Tasks.Task> -> unit
Public Sub RegisterAsyncFieldValidator (ByRef fieldIdentifier As FieldIdentifier, validator As Func(Of CancellationToken, Task))

Parameters

fieldIdentifier
FieldIdentifier

Identifies the field being validated.

validator
Func<CancellationToken,Task>

Asynchronous validation logic that starts using the supplied cancellation token and returns the in-flight Task.

Exceptions

validator is null.

validator returned a null task.

Remarks

The EditContext owns the token source end to end; to also cancel from another source, link inside validator via CreateLinkedTokenSource(CancellationToken). Write validator as an async method so a pre-await throw is captured into the task rather than thrown from this method. If validator throws or returns null, any prior validation for the field is still superseded before the exception propagates. Validators should clear prior messages for the field up-front and avoid writing partial results on a path that may throw.

Applies to