mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-02-11 03:09:07 +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()
|
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 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;
|
var dialogResult = await dialogReference.Result;
|
||||||
if (dialogResult.Canceled)
|
if (dialogResult.Canceled)
|
||||||
return;
|
return;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<MudDialog>
|
<MudDialog>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<MudForm @ref="@this.form" @bind-IsValid="@this.dataIsValid" @bind-Errors="@this.dataIssues">
|
<MudForm @ref="@this.form" @bind-IsValid="@this.dataIsValid" @bind-Errors="@this.dataIssues">
|
||||||
|
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||||
<MudTextField
|
<MudTextField
|
||||||
T="string"
|
T="string"
|
||||||
@bind-Text="@this.dataInstanceName"
|
@bind-Text="@this.dataInstanceName"
|
||||||
@ -12,7 +13,8 @@
|
|||||||
AdornmentIcon="@Icons.Material.Filled.Lightbulb"
|
AdornmentIcon="@Icons.Material.Filled.Lightbulb"
|
||||||
AdornmentColor="Color.Info"
|
AdornmentColor="Color.Info"
|
||||||
Required="@true"
|
Required="@true"
|
||||||
RequiredError="Please add an instance name."
|
RequiredError="Please enter an instance name."
|
||||||
|
Validation="@this.ValidatingInstanceName"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
using AIStudio.Provider;
|
using AIStudio.Provider;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
@ -10,6 +12,9 @@ public partial class ProviderDialog : ComponentBase
|
|||||||
{
|
{
|
||||||
[CascadingParameter]
|
[CascadingParameter]
|
||||||
private MudDialogInstance MudDialog { get; set; } = null!;
|
private MudDialogInstance MudDialog { get; set; } = null!;
|
||||||
|
|
||||||
|
[Parameter]
|
||||||
|
public IList<string> UsedInstanceNames { get; set; } = new List<string>();
|
||||||
|
|
||||||
private bool dataIsValid;
|
private bool dataIsValid;
|
||||||
private string[] dataIssues = [];
|
private string[] dataIssues = [];
|
||||||
@ -41,6 +46,31 @@ public partial class ProviderDialog : ComponentBase
|
|||||||
|
|
||||||
return null;
|
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();
|
private void Cancel() => this.MudDialog.Cancel();
|
||||||
|
|
||||||
|
[GeneratedRegex("^[a-zA-Z0-9 ]+$")]
|
||||||
|
private static partial Regex InstanceNameRegex();
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user