EditContext Class

Definition

Holds metadata related to a data editing process, such as flags to indicate which fields have been modified and the current set of validation messages.

public ref class EditContext sealed
public sealed class EditContext
type EditContext = class
Public NotInheritable Class EditContext
Inheritance
EditContext

Constructors

Name Description
EditContext(Object)

Constructs an instance of EditContext.

Properties

Name Description
Model

Gets the model object for this EditContext.

Properties

Gets a collection of arbitrary properties associated with this instance.

ShouldUseFieldIdentifiers

Gets whether field identifiers should be generated for <input> elements.

Methods

Name Description
AddValidationTask(FieldIdentifier, Task, CancellationTokenSource)

Registers an async validation task for a specific field. The task is tracked for pending/faulted state queries via IsValidationPending(FieldIdentifier) and IsValidationFaulted(FieldIdentifier). If a task is already tracked for this field, the previously registered CancellationTokenSource is cancelled and the new task replaces it. The EditContext takes ownership of the supplied cts: it will be cancelled if a subsequent validation supersedes this one, and disposed once task completes.

Field(String)

Supplies a FieldIdentifier corresponding to a specified field name on this EditContext's Model.

GetValidationMessages()

Gets the current validation messages across all fields.

This method does not perform validation itself. It only returns messages determined by previous validation actions.

GetValidationMessages(Expression<Func<Object>>)

Gets the current validation messages for the specified field.

This method does not perform validation itself. It only returns messages determined by previous validation actions.

GetValidationMessages(FieldIdentifier)

Gets the current validation messages for the specified field.

This method does not perform validation itself. It only returns messages determined by previous validation actions.

IsModified()

Determines whether any of the fields in this EditContext have been modified.

IsModified(Expression<Func<Object>>)

Determines whether the specified fields in this EditContext has been modified.

IsModified(FieldIdentifier)

Determines whether the specified fields in this EditContext has been modified.

IsValid(Expression<Func<Object>>)

Determines whether the specified fields in this EditContext has no associated validation messages.

IsValid(FieldIdentifier)

Determines whether the specified fields in this EditContext has no associated validation messages.

IsValidationFaulted()

Returns true if the most recent ValidateAsync(CancellationToken) pass observed an unhandled exception from any OnValidationRequestedAsync handler. A subsequent successful ValidateAsync(CancellationToken) pass clears the flag; a caller-cancelled pass preserves it. Use this to detect that validation itself failed (not just produced validation messages). For per-field validator faults from AddValidationTask(FieldIdentifier, Task, CancellationTokenSource), use the IsValidationFaulted(FieldIdentifier) overload.

IsValidationFaulted(FieldIdentifier)

Returns true if the specified field's last async validation faulted (threw a non-cancellation exception).

IsValidationFaulted<TField>(Expression<Func<TField>>)

Returns true if the field identified by the accessor expression's last async validation faulted (threw a non-cancellation exception).

IsValidationPending()

Returns true if a form-level ValidateAsync(CancellationToken) pass is currently in flight. Suitable for driving form-wide UI such as disabling a submit button or showing a "validating..." indicator for the current submission. Does not consider field-level pending tasks (those are superseded when the next form-level pass starts); use the IsValidationPending(FieldIdentifier) overload for per-field state.

IsValidationPending(FieldIdentifier)

Returns true if the specified field has a pending async validation task. A task is "pending" until the framework's observer has settled its outcome and cleared the slot (i.e., not only until the task itself completes) so a consumer that waits for IsValidationPending(FieldIdentifier) to become false is guaranteed to also see the final IsValidationFaulted(FieldIdentifier) value.

IsValidationPending<TField>(Expression<Func<TField>>)

Returns true if the field identified by the accessor expression has a pending async validation task.

MarkAsUnmodified()

Clears all modification flags within this EditContext.

MarkAsUnmodified(FieldIdentifier)

Clears any modification flag that may be tracked for the specified field.

NotifyFieldChanged(FieldIdentifier)

Signals that the value for the specified field has changed.

NotifyValidationStateChanged()

Signals that some aspect of validation state has changed.

Validate()

Validates this EditContext.

ValidateAsync(CancellationToken)

Validates this EditContext asynchronously. Cancels any pending field-level async validation tasks, invokes the synchronous OnValidationRequested handlers, then invokes and awaits the asynchronous OnValidationRequestedAsync handlers concurrently. Exceptions from synchronous handlers propagate to the caller, matching Validate(). Any non-cancellation exception thrown by an asynchronous handler is contained: the form is marked as faulted (observable via the parameterless IsValidationFaulted()) and the method returns false. While the asynchronous portion is in flight, the parameterless IsValidationPending() returns true so applications can show a global "validating..." indicator without wrapping the call themselves. The form-level IsValidationFaulted() result is updated only when a pass completes; it is preserved across caller-cancelled passes.

Events

Name Description
OnFieldChanged

An event that is raised when a field value changes.

OnValidationRequested

An event that is raised when validation is requested. Validator components subscribe to this event to perform synchronous validation.

OnValidationRequestedAsync

An async event that is raised when validation is requested. Validator components subscribe to this event to perform async validation (e.g., database lookups, remote API calls). Handlers are awaited by ValidateAsync(CancellationToken). Validate() also invokes these handlers but requires each to complete synchronously; if any returns an incomplete Task, Validate() throws InvalidOperationException.

OnValidationStateChanged

An event that is raised when validation state has changed.

Extension Methods

Name Description
AddDataAnnotationsValidation(EditContext)
Obsolete.

Adds DataAnnotations validation support to the EditContext.

EnableDataAnnotationsValidation(EditContext, IServiceProvider)

Enables DataAnnotations validation support for the EditContext.

EnableDataAnnotationsValidation(EditContext)
Obsolete.

Enables DataAnnotations validation support for the EditContext.

FieldCssClass(EditContext, FieldIdentifier)

Gets a string that indicates the status of the specified field as a CSS class.

FieldCssClass<TField>(EditContext, Expression<Func<TField>>)

Gets a string that indicates the status of the specified field as a CSS class. This will include some combination of "modified", "valid", or "invalid", depending on the status of the field.

SetFieldCssClassProvider(EditContext, FieldCssClassProvider)

Associates the supplied FieldCssClassProvider with the supplied EditContext. This customizes the field CSS class names used within the EditContext.

Applies to