48 lines
1.3 KiB
Plaintext
48 lines
1.3 KiB
Plaintext
@implements IDisposable
|
|
|
|
<DataAnnotationsValidator />
|
|
|
|
@code {
|
|
[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;
|
|
}
|
|
}
|
|
}
|