Skip to content

Add object-level validation APIs to EditContext#67178

Open
VigneshwaranGovindharajan wants to merge 4 commits into
dotnet:mainfrom
VigneshwaranGovindharajan:forms/40306-editcontext-object-validation
Open

Add object-level validation APIs to EditContext#67178
VigneshwaranGovindharajan wants to merge 4 commits into
dotnet:mainfrom
VigneshwaranGovindharajan:forms/40306-editcontext-object-validation

Conversation

@VigneshwaranGovindharajan

Copy link
Copy Markdown

Add object-level validation APIs to EditContext

Description

Adds support for querying validation messages and modification state at the model object level in EditContext. This enables consumers to determine validation status for specific nested objects (child models), which is currently not possible using existing APIs.
Today, validation messages can be retrieved either globally or per field, but there is no built-in way to group or filter them by object instance. This makes it difficult to build complex forms (for example, tabbed or sectioned UIs) where validation needs to be associated with a specific sub-model.
This change introduces APIs that allow filtering validation state using the model instance that owns the fields.

Changes:

  • Added new APIs to EditContext:

  • IEnumerable GetValidationMessages(object model);

  • bool IsModified(object model);

  • void MarkAsUnmodified(object model);

  • Behavior:

  • GetValidationMessages(object model) returns all validation messages for fields whose FieldIdentifier.Model matches the provided object.

  • IsModified(object model) returns true if any field belonging to the specified object has been modified.

  • MarkAsUnmodified(object model) resets the modified state for all fields of the specified object.

  • Implementation details:

  • Iterates over internal _fieldStates and filters entries using: ReferenceEquals(fieldIdentifier.Model, model)

  • No additional storage or indexing introduced.

  • Preserves existing validation behavior without changes.

Usage Example

`var errors = editContext.GetValidationMessages(model.Person);

if (errors.Any())
{
// Mark Person section/tab as invalid
}`

Tests

Added unit tests covering:

  • Retrieval of validation messages across multiple stores for a specific object.
  • Filtering behavior across different model instances.
  • Handling of null and empty results.
  • Duplicate message behavior.
  • Object-level modification tracking (IsModified, MarkAsUnmodified).
  • Integration with Validate() and ValidateAsync() for nested object validation scenarios.

Fixes #40306

@VigneshwaranGovindharajan VigneshwaranGovindharajan requested a review from a team as a code owner June 12, 2026 10:58
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @VigneshwaranGovindharajan. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API-Proposal: Add functions to EditContext to filter Validationmessages based on Model property in FieldIdentifier

2 participants