Added instance name validation

This commit is contained in:
Thorsten Sommer 2024-04-19 23:27:38 +02:00
parent cc75df4a15
commit 62e16f2897
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 39 additions and 2 deletions

View File

@ -31,8 +31,13 @@ public partial class Settings : ComponentBase
private async Task AddProvider()
{
var dialogParameters = new DialogParameters<ProviderDialog>
{
{ x => x.UsedInstanceNames, this.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList() },
};
var dialogOptions = new DialogOptions { CloseOnEscapeKey = true, FullWidth = true, MaxWidth = MaxWidth.Medium };
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Add Provider", dialogOptions);
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Add Provider", dialogParameters, dialogOptions);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;

View File

@ -4,6 +4,7 @@
<MudDialog>
<DialogContent>
<MudForm @ref="@this.form" @bind-IsValid="@this.dataIsValid" @bind-Errors="@this.dataIssues">
@* ReSharper disable once CSharpWarnings::CS8974 *@
<MudTextField
T="string"
@bind-Text="@this.dataInstanceName"
@ -12,7 +13,8 @@
AdornmentIcon="@Icons.Material.Filled.Lightbulb"
AdornmentColor="Color.Info"
Required="@true"
RequiredError="Please add an instance name."
RequiredError="Please enter an instance name."
Validation="@this.ValidatingInstanceName"
/>
@* ReSharper disable once CSharpWarnings::CS8974 *@

View File

@ -1,3 +1,5 @@
using System.Text.RegularExpressions;
using AIStudio.Provider;
using Microsoft.AspNetCore.Components;
@ -10,6 +12,9 @@ public partial class ProviderDialog : ComponentBase
{
[CascadingParameter]
private MudDialogInstance MudDialog { get; set; } = null!;
[Parameter]
public IList<string> UsedInstanceNames { get; set; } = new List<string>();
private bool dataIsValid;
private string[] dataIssues = [];
@ -41,6 +46,31 @@ public partial class ProviderDialog : ComponentBase
return null;
}
private string? ValidatingInstanceName(string instanceName)
{
if(instanceName.StartsWith(' '))
return "The instance name must not start with a space.";
if(instanceName.EndsWith(' '))
return "The instance name must not end with a space.";
// The instance name must only contain letters, numbers, and spaces:
if (!InstanceNameRegex().IsMatch(instanceName))
return "The instance name must only contain letters, numbers, and spaces.";
if(instanceName.Contains(" "))
return "The instance name must not contain consecutive spaces.";
// The instance name must be unique:
if (this.UsedInstanceNames.Contains(instanceName.ToLowerInvariant()))
return "The instance name must be unique; the chosen name is already in use.";
return null;
}
private void Cancel() => this.MudDialog.Cancel();
[GeneratedRegex("^[a-zA-Z0-9 ]+$")]
private static partial Regex InstanceNameRegex();
}