Skip to content
Form FormField

FormField

Label, description, hint and validation message wired to the control inside.

On this page

Examples

A complete form

We never share it.

Optional
Developer
Designer
Product manager

Markdown is supported.

Razor
@using System.ComponentModel.DataAnnotations

<EditForm Model="Model" FormName="signup" OnValidSubmit="Submit" class="flex w-full max-w-sm flex-col gap-4" novalidate>
    <DataAnnotationsValidator />
    <KlyFormField Label="Email" Required Field="FieldIdentifier.Create(() => Model!.Email)" Description="We never share it.">
        <KlyInput Type="InputType.Email" @bind-Value="Model!.Email" Placeholder="you@example.com" />
    </KlyFormField>
    <KlyFormField Label="Role" Hint="Optional">
        <KlySelect @bind-Value="Model!.Role" Items="Roles" Placeholder="Pick one" />
    </KlyFormField>
    <KlyFormField Label="About you" Help="Markdown is supported." Field="FieldIdentifier.Create(() => Model!.Bio)">
        <KlyTextarea @bind-Value="Model!.Bio" Rows="3" />
    </KlyFormField>
    <KlyCheckbox @bind-Value="Model!.Newsletter" Label="Send me the newsletter" />
    <KlyButton Type="ButtonType.Submit" Label="Create account" Block />
    @if (_done)
    {
        <KlyAlert Title="Account created" Color="KlyColor.Success" Variant="KlyVariant.Soft" Icon='KlyIcons.Named("circle-check")' />
    }
</EditForm>

@code {
    [SupplyParameterFromForm(FormName = "signup")]
    private SignUp? Model { get; set; }

    private bool _done;

    private static readonly KlySelectItem[] Roles = [new("dev", "Developer"), new("design", "Designer"), new("pm", "Product manager")];

    protected override void OnInitialized() => Model ??= new();

    private void Submit() => _done = true;

    private sealed class SignUp
    {
        [Required, EmailAddress]
        public string? Email { get; set; }

        public string? Role { get; set; }

        [MaxLength(200)]
        public string? Bio { get; set; }

        public bool Newsletter { get; set; }
    }
}

API

KlyFormField

Wrapper that gives a control its label, description, hint and error, and ties them together with the right ids for accessibility.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
Description string Text shown under the label and tied to the control with aria-describedby .
Error string Error text. Without it, the first validation message of Field is shown.
Field FieldIdentifier? The bound field, for validation messages inside an EditForm : Field="FieldIdentifier.Create(() => Model.Email)" (or new FieldIdentifier(Model, nameof(Model.Email)) ). It names the same field the control inside is bound to, so the message under the control is the one the control's own error styling follows.
Help string Help text shown under the control.
Hint string Short text next to the label, such as "Optional".
Id string Id of the control inside. Defaults to a generated one.
Label string Label of the control, tied to it with for .
Required bool false Marks the label and the control as required.
Size KlySize KlySize.Md Size passed to the controls inside, from Xs to Xl . Values: Xs, Sm, Md, Lg, Xl.
Ui FormFieldUi Per-instance class overrides of the slots of the form field, see FormFieldUi . They are merged after the theme and the Class parameter wins on the root slot.

Slots

Name Type Description
ChildContent RenderFragment The control, such as a KlyInput .

Ui slots

Ui takes a FormFieldUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyFormField , after the theme and the app configuration.
LabelWrapper Classes merged into the labelWrapper slot of KlyFormField , after the theme and the app configuration.
Label Classes merged into the label slot of KlyFormField , after the theme and the app configuration.
Hint Classes merged into the hint slot of KlyFormField , after the theme and the app configuration.
Description Classes merged into the description slot of KlyFormField , after the theme and the app configuration.
Container Classes merged into the container slot of KlyFormField , after the theme and the app configuration.
Error Classes merged into the error slot of KlyFormField , after the theme and the app configuration.
Help Classes merged into the help slot of KlyFormField , after the theme and the app configuration.