48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using BudgetApp.Services;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
|
|
namespace BudgetApp.Components;
|
|
|
|
public partial class ToastValidator
|
|
{
|
|
[CascadingParameter] private EditContext? CurrentEditContext { get; set; }
|
|
|
|
[Inject] private ToastService ToastService { get; set; } = default!;
|
|
|
|
private bool _validationRequested;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
if (CurrentEditContext is not null)
|
|
{
|
|
CurrentEditContext.OnValidationRequested += OnValidationRequested;
|
|
CurrentEditContext.OnValidationStateChanged += OnValidationStateChanged;
|
|
}
|
|
}
|
|
|
|
private void OnValidationRequested(object? sender, ValidationRequestedEventArgs e)
|
|
{
|
|
_validationRequested = true;
|
|
}
|
|
|
|
private void OnValidationStateChanged(object? sender, ValidationStateChangedEventArgs e)
|
|
{
|
|
if (!_validationRequested || CurrentEditContext is null) return;
|
|
_validationRequested = false;
|
|
|
|
foreach (var message in CurrentEditContext.GetValidationMessages())
|
|
{
|
|
ToastService.Show(message);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (CurrentEditContext is not null)
|
|
{
|
|
CurrentEditContext.OnValidationRequested -= OnValidationRequested;
|
|
CurrentEditContext.OnValidationStateChanged -= OnValidationStateChanged;
|
|
}
|
|
}
|
|
} |