Skip to content

FormPage

Renders a form whose fields are declared in sections as a page: a sidebar lists each section with its status (complete, pending, invalid or optional) and each section is a card.

On this page

Examples

Settings page with sections

A section marked optional shows Optional while it is empty. The page is a form element, so pressing Enter in a field submits it. Fields render with the same controls as the form engine, and the schema must declare sections.

Workspace settings

Everything is ready.

General

Notifications

Security

Razor
<div class="w-full max-w-4xl">
    <KlyFormPage Schema="Schema" Model="_settings" OnSave="SaveAsync" />
</div>

@code {
    private readonly Settings _settings = new() { Name = "Acme", Domain = "acme.example", SessionMinutes = 60 };

    private static readonly FormSchema<Settings> Schema = FormSchema.For<Settings>(s =>
    {
        s.FormKey = "settings";
        s.Title = "Workspace settings";
        s.Section("general", "General", section =>
        {
            section.Field(m => m.Name, f => f.Label("Workspace name").Required());
            section.Field(m => m.Domain, f => f.Label("Domain").Placeholder("acme.example")
                .Required().Rule(v => v.Contains('.'), "Enter a domain such as acme.example"));
        });
        FormField<Settings, bool> notifications = null!;
        s.Section("notifications", "Notifications", section =>
        {
            notifications = section.Field(m => m.Notifications, f => f.Label("Send notifications").AsSwitch());
            section.Field(m => m.SlackChannel, f => f.Label("Slack channel").Placeholder("#general")
                .VisibleWhen(c => c.Get(notifications)).Required(c => c.Get(notifications)).DependsOn(notifications));
        }, optional: true);
        s.Section("security", "Security", section =>
        {
            section.Field(m => m.TwoFactor, f => f.Label("Require two-factor sign-in").AsSwitch());
            section.Field(m => m.SessionMinutes, f => f.Label("Session length (minutes)").Range(5, 240));
        }, optional: true);
    });

    private Task<bool> SaveAsync(Settings settings, CancellationToken cancellationToken) => Task.FromResult(true);

    public sealed class Settings
    {
        public string Name { get; set; } = "";
        public string Domain { get; set; } = "";
        public bool Notifications { get; set; }
        public string SlackChannel { get; set; } = "";
        public bool TwoFactor { get; set; }
        public int SessionMinutes { get; set; } = 60;
    }
}

API

KlyFormPage<TModel>

Renders a sectioned typed form as a page: a sidebar listing each section with its completion status, and one card per section laid out by its span. Fields render with the same controls, labels, containers and repeatable lists as KlyFormEngine<TModel> , and the page is a form element, so pressing Enter in a field submits it. is inferred from Form , Schema or Model .

Parameters

Name Type Description
Class string Classes added to the root element, merged last so they win over the theme.
Form FormController<TModel> The controller to render. Its schema must declare sections ( Section ). Takes precedence over Schema .
Model TModel The model to edit. Its values are the form's starting values, and a submit writes the values back into it (all or nothing, put back when a step fails), so members the form does not show keep their value. Without a model every submit creates a new . A different instance reloads the form, dropping unsaved edits.
OnSave Func<TModel, CancellationToken, Task<bool>> Your save step: called with the model after validation and the schema hooks passed, while the form is still submitting. Return true when the save succeeded; the form is then clean and OnSubmit is raised. Return false to keep the edits dirty, after reporting the reason with Boolean) or Boolean) . The token is cancelled when the component is disposed.
Schema FormSchema<TModel> The schema to render when no Form is given. It must declare sections. The component creates and owns the controller, and creates a new one when the schema instance changes.

Events

Name Type Description
OnSubmit EventCallback<TModel> Called with the model after the form is submitted successfully, that is after validation, the schema hooks and OnSave passed. The submission is over at that point and the form is clean.

Slots

Name Type Description
ChildContent RenderFragment Templates of the fields ( KlyFieldTemplate ). They draw nothing themselves: they hand their control to the field they name.