Field

Label, control, description and validation message as one unit.

Field is new in v0.4.0. It wraps one form control with its label, help text and validation message, stacked or side by side, so a form doesn't need the separate Form field parts for the common case.

Installation

bash
shellui add field

This also installs form-message, which shows the messages.

Usage

With validation

Inside an EditForm, point For at the model property and Field shows that property's validation messages. LabelFor takes the control's id, so clicking the label focuses it, and Required adds a red asterisk.

We only use it to sign you in.

@using System.ComponentModel.DataAnnotations

<div class="w-full max-w-sm">
    <EditForm Model="model" OnValidSubmit="@(() => saved = true)" OnInvalidSubmit="@(() => saved = false)" class="grid gap-4">
        <DataAnnotationsValidator />

        <Field Label="Email" LabelFor="field-email" Required="true" Description="We only use it to sign you in." For="() => model.Email">
            <Input id="field-email" @bind-Value="model.Email" Placeholder="you@example.com" />
        </Field>

        <Field Label="Display name" LabelFor="field-name" For="() => model.Name">
            <InputText id="field-name" @bind-Value="model.Name" class="flex h-9 w-full rounded-md border border-input bg-background px-3 py-1 text-sm" />
        </Field>

        <div class="flex items-center gap-3">
            <Button Type="submit">Create account</Button>
            @if (saved)
            {
                <span class="text-sm text-muted-foreground">Saved @model.Email.</span>
            }
        </div>
    </EditForm>
</div>

@code {
    private readonly Account model = new();
    private bool saved;

    private sealed class Account
    {
        [Required(ErrorMessage = "Enter your email.")]
        [EmailAddress(ErrorMessage = "That doesn't look like an email address.")]
        public string Email { get; set; } = "";

        [Required(ErrorMessage = "Enter a display name.")]
        [MinLength(3, ErrorMessage = "Use at least 3 characters.")]
        public string Name { get; set; } = "";
    }
}

Messages appear when the form is submitted. With Blazor's own inputs (InputText and the rest), which tell the EditForm about each change, they also update as the user types; ShellUI's Input doesn't, so its messages change on the next submit.

Horizontal

Orientation="horizontal" puts the label and description beside the control, for switches and checkboxes.

A short summary every Monday.

Emails about new releases.

<div class="grid w-full max-w-sm gap-6">
    <Field Label="Weekly digest" Description="A short summary every Monday." Orientation="horizontal">
        <Switch @bind-Checked="digest" />
    </Field>
    <Field Label="Product updates" Description="Emails about new releases." Orientation="horizontal">
        <Checkbox @bind-Checked="updates" />
    </Field>
</div>

@code {
    private bool digest = true;
    private bool updates;
}

Your own error

Error shows a message of your own instead of the EditForm's, for checks done outside data annotations.

That username is taken.

<div class="w-full max-w-sm">
    <Field Label="Username" LabelFor="field-username" Error="@error">
        <Input id="field-username" @bind-Value="username" Variant="@(error is null ? InputVariant.Default : InputVariant.Error)" />
    </Field>
</div>

@code {
    private string username = "admin";

    private string? error => username.Trim().ToLowerInvariant() is "admin" or "root" ? "That username is taken." : null;
}

API Reference

Parameter Type Default Description
Label string? null The label text
LabelFor string? null The id of the control inside, rendered as the label's for
Description string? null Help text below the control (beside it when horizontal)
Error string? null A message shown instead of the EditForm's validation messages
For Expression<Func<object?>>? null The model property whose validation messages to show, such as () => model.Email
Required bool false Adds an asterisk after the label (hidden from screen readers)
Orientation string "vertical" vertical, or horizontal for the label beside the control
ChildContent RenderFragment? null The control
Class string? null Additional CSS classes

Accessibility

  • The wrapper has role="group"
  • Set LabelFor and give the control the same id so the label names it
  • Required only marks the label. Mark the control itself too (a required attribute or a [Required] rule) so it is enforced

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.