Form
A thin wrapper over EditForm: validation summary and a busy state while submitting.
On this page
Examples
Busy while submitting
Razor
@using System.ComponentModel.DataAnnotations
<KlyForm Model="Model" FormName="kly-form" OnValidSubmit="SaveAsync" ShowSummary class="flex w-full max-w-sm flex-col">
<KlyFormField Label="Name" Required Field="FieldIdentifier.Create(() => Model!.Name)">
<KlyInput @bind-Value="Model!.Name" />
</KlyFormField>
<KlyFormField Label="Seats" Field="FieldIdentifier.Create(() => Model!.Seats)">
<KlyInputNumber @bind-Value="Model!.Seats" Min="1" Max="50" />
</KlyFormField>
<KlyButton Type="ButtonType.Submit" Label="Create workspace" />
@if (_saved)
{
<p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="form-saved">Workspace @Model!.Name created.</p>
}
</KlyForm>
@code {
[SupplyParameterFromForm(FormName = "kly-form")]
private Workspace? Model { get; set; }
private bool _saved;
protected override void OnInitialized() => Model ??= new();
private async Task SaveAsync()
{
// Stands for a slow call: the form is busy and its controls are disabled meanwhile.
await Task.Delay(300);
_saved = true;
}
private sealed class Workspace
{
[Required(ErrorMessage = "The name is required.")]
public string? Name { get; set; }
[Range(1, 50)]
public decimal? Seats { get; set; } = 5;
}
}Enhanced post
Static rendering with Enhance: the post updates the page in place, and what the server renders back wins over what the browser showed.
Razor
@using System.ComponentModel.DataAnnotations
<KlyForm Model="Model" FormName="enhanced-form" Enhance OnValidSubmit="Save" class="flex w-full max-w-sm flex-col">
<KlyFormField Label="Stack" Field="FieldIdentifier.Create(() => Model!.Stack)">
<KlySelectMenu Items="Stack" Multiple @bind-Value="Model!.Stack" Placeholder="Pick technologies" />
</KlyFormField>
<KlyFormField Label="Marks" Field="FieldIdentifier.Create(() => Model!.Marks)">
<KlyToggleGroup Items="Marks" AriaLabel="Marks" Multiple @bind-Value="Model!.Marks" />
</KlyFormField>
<KlyFormField Label="Seats" Field="FieldIdentifier.Create(() => Model!.Seats)">
<KlyInputNumber @bind-Value="Model!.Seats" Min="0" Max="50" />
</KlyFormField>
<KlyFormField Label="Code" Field="FieldIdentifier.Create(() => Model!.Code)">
<KlyPinInput Length="4" @bind-Value="Model!.Code" />
</KlyFormField>
<KlyFormField Label="Visit" Field="FieldIdentifier.Create(() => Model!.Visit)">
<KlyCalendar @bind-Value="Model!.Visit" AriaLabel="Visit date" />
</KlyFormField>
<KlyButton Type="ButtonType.Submit" Label="Save enhanced" />
@if (_posted is not null)
{
<p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="enhanced-saved">Posted: @_posted</p>
}
</KlyForm>
@code {
[SupplyParameterFromForm(FormName = "enhanced-form")]
private Profile? Model { get; set; }
private string? _posted;
private static readonly KlySelectItem[] Stack = [new("dotnet", ".NET"), new("node", "Node.js"), new("go", "Go")];
private static readonly KlyToggleItem[] Marks = [new("bold", "Bold"), new("italic", "Italic")];
protected override void OnInitialized() => Model ??= new();
// The server resets the model after saving: the page it sends back must win over what the browser showed.
private void Save()
{
_posted = $"{string.Join(",", Model!.Stack)} / {string.Join(",", Model.Marks)} / {Model.Seats} / {Model.Code} / {Model.Visit:yyyy-MM-dd}";
Model = new();
}
private sealed class Profile
{
public IReadOnlyList<string> Stack { get; set; } = ["dotnet"];
public IReadOnlyList<string> Marks { get; set; } = ["bold"];
public decimal? Seats { get; set; } = 2;
public string? Code { get; set; }
public DateOnly? Visit { get; set; } = new(2026, 11, 10);
}
}API
KlyForm
Form that wraps an EditForm , gives its fields the edit context and can show a summary of the validation errors.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Disabled |
bool |
false |
Disables every control inside. |
EditContext |
EditContext |
The edit context to use. Give either this or Model . | |
Enhance |
bool |
false |
Enhanced form handling in static server rendering: the post updates the page without a reload. |
FormName |
string |
Name of the form in static server rendering, where it identifies the posted form. | |
Model |
object |
The object edited. Give either this or EditContext . | |
ShowSummary |
bool |
false |
Shows the list of every validation message above the fields. |
Ui |
FormUi |
Per-instance class overrides of the slots of the form, see FormUi . They are merged after the theme and the Class parameter wins on the root slot. | |
Validate |
bool |
true |
Runs data annotation validation on submit and on field changes. |
Events
| Name | Type | Description |
|---|---|---|
OnInvalidSubmit |
EventCallback |
Raised with the edit context when the form is submitted and validation fails. |
OnValidSubmit |
EventCallback |
Called with a valid form. While its task runs the form is busy and its controls are disabled. |
Slots
| Name | Type | Description |
|---|---|---|
ChildContent |
RenderFragment |
The fields of the form, with the edit context as the context value. |
Ui slots
Ui takes a FormUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyForm , after the theme and the app configuration. |
Fieldset |
Classes merged into the fieldset slot of KlyForm , after the theme and the app configuration. |
Summary |
Classes merged into the summary slot of KlyForm , after the theme and the app configuration. |
SummaryList |
Classes merged into the summaryList slot of KlyForm , after the theme and the app configuration. |