mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-02-10 17:29:05 +00:00
Added instance name validation
This commit is contained in:
parent
cc75df4a15
commit
62e16f2897
@ -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;
|
||||
|
@ -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 *@
|
||||
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user