Add FormValidationService and EventDefinitionService to dependency injection
Enhanced the application's service layer by adding FormValidationService and EventDefinitionService to the dependency injection container in Program.cs. Updated Create, Edit, and other relevant components to utilize these services for improved form validation and event processing functionality.
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
@page "/students/create"
|
||||
@attribute [Authorize]
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using MudBlazor
|
||||
@using WebApp.Services
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@inject AppDbContext Context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject FormValidationService ValidationService
|
||||
|
||||
<PageHeader
|
||||
Title="Create"
|
||||
@@ -10,10 +15,12 @@
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="@(ReturnUrl ?? "/students")" />
|
||||
|
||||
<EditForm Model="Student" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<EditForm EditContext="@_editContext" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
<ValidationErrorDisplay Errors="_validationErrors" />
|
||||
|
||||
<MudPaper Elevation="2" Class="pa-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Student Information</MudText>
|
||||
@@ -53,14 +60,45 @@
|
||||
private Student Student { get; set; } = new() { TsaYear = 1 };
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
private void OnValidSubmit(EditContext context)
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
_editContext = new EditContext(Student);
|
||||
}
|
||||
|
||||
Context.Students.Add(Student);
|
||||
Context.SaveChanges();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Student, errors => _validationErrors = errors);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task OnValidSubmit()
|
||||
{
|
||||
_validationErrors.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
Context.Students.Add(Student);
|
||||
await Context.SaveChangesAsync();
|
||||
|
||||
Snackbar.Add($"Student '{Student.FirstNameLastName}' created successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while creating the student.",
|
||||
"student",
|
||||
"creating");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCancel()
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
@attribute [Authorize]
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using MudBlazor
|
||||
@using WebApp.Services
|
||||
@inject AppDbContext Context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject FormValidationService ValidationService
|
||||
|
||||
@if (Student is null)
|
||||
@if (Student is null || _editContext is null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
return;
|
||||
@@ -19,10 +23,13 @@
|
||||
|
||||
@* https://www.mudblazor.com/components/form *@
|
||||
@* https://medium.com/@husainalbar/applying-mudblazor-for-crud-operations-in-our-blazor-project-a343037a52ef *@
|
||||
<EditForm method="post" Model="Student" OnValidSubmit="UpdateStudent" FormName="edit" Enhance>
|
||||
<EditForm method="post" EditContext="@_editContext" OnInvalidSubmit="OnInvalidSubmit" FormName="edit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator/>
|
||||
|
||||
<ValidationErrorDisplay Errors="_validationErrors" />
|
||||
|
||||
<MudGrid>
|
||||
<MudItem xs="12" sm="7">
|
||||
<MudPaper Elevation="2" Class="pa-6">
|
||||
@@ -64,6 +71,8 @@
|
||||
private Student? Student { get; set; }
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@@ -73,12 +82,36 @@
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
}
|
||||
else
|
||||
{
|
||||
_editContext = new EditContext(Student);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Student!, errors => _validationErrors = errors);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
// To protect from overposting attacks, enable the specific properties you want to bind to.
|
||||
// For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
|
||||
private async Task UpdateStudent()
|
||||
{
|
||||
// Validate before processing
|
||||
if (_editContext != null)
|
||||
{
|
||||
var errors = ValidationService.CollectValidationErrors(_editContext, Student!);
|
||||
if (errors.Any())
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(_editContext, Student!, err => _validationErrors = err);
|
||||
StateHasChanged();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_validationErrors.Clear();
|
||||
|
||||
if (Student?.OfficerRole == 0)
|
||||
Student.OfficerRole = null;
|
||||
|
||||
@@ -87,20 +120,33 @@
|
||||
try
|
||||
{
|
||||
await Context.SaveChangesAsync();
|
||||
Snackbar.Add($"Student '{Student!.FirstNameLastName}' saved successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
catch (DbUpdateConcurrencyException ex)
|
||||
{
|
||||
if (!StudentExists(Student!.Id))
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
}
|
||||
else
|
||||
if (!ValidationService.HandleDbUpdateConcurrencyException(
|
||||
ex,
|
||||
() => StudentExists(Student!.Id),
|
||||
"student",
|
||||
() => NavigationManager.NavigateTo("notfound")))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while saving the student.",
|
||||
"student",
|
||||
"saving");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleCancel()
|
||||
|
||||
Reference in New Issue
Block a user