| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using Microsoft.AspNetCore.Components;
- using Microsoft.AspNetCore.Components.Forms;
- namespace BlazorEditForms;
- public class CustomFluentValidator : ComponentBase, IDisposable
- {
- private IDisposable? _subscriptions;
- private EditContext? _originalEditContext;
- [CascadingParameter] EditContext? CurrentEditContext { get; set; }
- [Inject] private IServiceProvider ServiceProvider { get; set; } = default!;
- protected override void OnInitialized()
- {
- if (CurrentEditContext == null)
- {
- throw new InvalidOperationException($"{nameof(DataAnnotationsValidator)} requires a cascading " +
- $"parameter of type {nameof(EditContext)}. For example, you can use {nameof(DataAnnotationsValidator)} " +
- $"inside an EditForm.");
- }
- _subscriptions = new CustomFluentValidationManager(CurrentEditContext, ServiceProvider);
- _originalEditContext = CurrentEditContext;
- }
- protected override void OnParametersSet()
- {
- if (CurrentEditContext != _originalEditContext)
- {
- // While we could support this, there's no known use case presently. Since InputBase doesn't support it,
- // it's more understandable to have the same restriction.
- throw new InvalidOperationException($"{GetType()} does not support changing the " +
- $"{nameof(EditContext)} dynamically.");
- }
- }
- public void Dispose()
- {
- _subscriptions?.Dispose();
- _subscriptions = null;
- }
- }
|