dotnet / dotnet/aspnetcore

Blazor EditContext OnValidationStageChanged for single FieldIdentifiers

Open
#44,284 2 comments 0 reactions 0 assignees View on GitHub
area-blazor enhancement feature-blazor-builtin-components
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I would like to be able to handle events if the validation state has changed for a single `FieldIdentifier`

### Describe the solution you'd like

It would make most sense to add the `FieldIdentifier` to the `ValidationStateChangedEventArgs` that could be set whenever the `NotifyValidationStateChanged` method on the `EditContext` is invoked with a `FieldIdentifier`

The default behaviour could still be an empty event for the whole model but it would be fantastic if you could pass in a range of or an individual `FieldIdentifier`

In the `EditContext`:

```cs
public void NotifyValidationStateChanged(in FieldIdentifier fieldIdentifier)
{
OnValidationStateChanged?.Invoke(this, new ValidationStateHasChangedEventArgs(fieldIdentifier));
}
```

And the `ValidationStateHasChangedEventsArgs` being updated to include an additional constructor to take in the `FieldIdentifier`:

```csharp
public sealed class ValidationStateHasChangedEventsArgs : EventArgs
{
// ... existing constructor and static empty instance

public ValidationStateHasChangedEventArgs(in FieldIdentifier fieldIdentifier)
{
FieldIdentifier = fieldIdentifier;
}

public FieldIdentifier? FieldIdentifier { get; }
}
```

### Additional context

I believe this will improve performance of handling the `OnValidationStateChanged` events by allowing components / code that hooks onto these events to filter whether it needs to process this event or not.

In my application I am hooking into the `OnFieldChanged` and `OnValidationRequested` to use `FluentValidation` to update the `EditContext` with my validation rules then calling the `NotifyValidationStateHasChanged`. In the instance of just handling field changes, I think it makes sense to notify that validation for that (or dependent fields I am aware of) are only notified of the validation state changed. Across my form I have implemented a custom component that displays my validation message for the component it is associated.

This could also be used to enhance the `Microsoft.AspNetCore.Components.Forms.ValidationMessage` component by only handling changes when the whole form is updated or just the individual field.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.