mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-13 20:53:48 +00:00
Improved input validation for the single input dialog (#541)
This commit is contained in:
1 parent
e931e28ab1
commit
99dc2606aa
7 files changed
+77
-18
No files matched your search
@@ -4,7 +4,9 @@
|
||||
<MudText Typo="Typo.body1">
|
||||
@this.Message
|
||||
</MudText>
|
||||
<MudTextField T="string" @bind-Text="@this.UserInput" Variant="Variant.Outlined" AutoGrow="@false" Lines="1" Label="@T("Chat name")" AutoFocus="@true" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
|
||||
<MudForm @ref="this.form" Class="mt-4">
|
||||
<MudTextField T="string" @bind-Text="@this.UserInput" Variant="Variant.Outlined" AutoGrow="@false" Lines="1" Label="@T("Chat name")" AutoFocus="@true" UserAttributes="@USER_INPUT_ATTRIBUTES" Validation="@this.ValidateUserInput" />
|
||||
</MudForm>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
|
||||
|
||||
@@ -20,9 +20,17 @@ public partial class SingleInputDialog : MSGComponentBase
|
||||
|
||||
[Parameter]
|
||||
public Color ConfirmColor { get; set; } = Color.Error;
|
||||
|
||||
|
||||
[Parameter]
|
||||
public bool AllowEmptyInput { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string EmptyInputErrorMessage { get; set; } = string.Empty;
|
||||
|
||||
private static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
|
||||
|
||||
private MudForm form = null!;
|
||||
|
||||
#region Overrides of ComponentBase
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
@@ -34,7 +42,22 @@ public partial class SingleInputDialog : MSGComponentBase
|
||||
|
||||
#endregion
|
||||
|
||||
private string? ValidateUserInput(string? value)
|
||||
{
|
||||
if (!this.AllowEmptyInput && string.IsNullOrWhiteSpace(value))
|
||||
return string.IsNullOrWhiteSpace(this.EmptyInputErrorMessage) ? T("Please enter a value.") : this.EmptyInputErrorMessage;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void Cancel() => this.MudDialog.Cancel();
|
||||
|
||||
private void Confirm() => this.MudDialog.Close(DialogResult.Ok(this.UserInput));
|
||||
private async Task Confirm()
|
||||
{
|
||||
await this.form.Validate();
|
||||
if(!this.form.IsValid)
|
||||
return;
|
||||
|
||||
this.MudDialog.Close(DialogResult.Ok(this.UserInput));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user