Skip to content

Overview

Schema-driven forms, validation and wizards for .NET developers, with a headless engine and Blazor components.

On this page

You declare a form from a C# model with FormSchema.For<TModel>: fields, conditions, rules, steps and sections, written with refactor-safe expressions and checked by the compiler. A FormController<TModel> holds its state: values typed as your members, touched fields, errors, dirty tracking and step navigation. The controller has no UI dependency, so the same schema can be unit tested, run in a view model, or rendered by the Blazor components. The Blazor layer builds on Klyroo.UI and runs in Interactive Server, WebAssembly and Auto render modes, and in .NET MAUI Blazor Hybrid.

Every page of the documentation uses the typed API except one. A form that only exists as data at runtime, such as one stored as JSON or in a database, uses the runtime mode in Klyroo.Form.Dynamic, which is documented apart in Forms defined at runtime.

Prerequisites

To use the library in an app:

  • The .NET 11 SDK, and an app that targets net11.0. The packages are prereleases, so every dotnet add package command needs --prerelease.
  • Klyroo.Form.Core alone (the form controller) runs in any .NET 11 app, with no other dependency.
  • A Blazor app for Klyroo.Form: a Blazor Web App or a .NET MAUI Blazor Hybrid app. Typing, validation feedback, steps and submit need an interactive render mode (Interactive Server, WebAssembly or Auto). In a WebAssembly or Auto app, register the services in the server project and in the .Client project.
  • Klyroo.UI set up in the app. Klyroo.Form brings the Klyroo.UI package, and AddKlyrooForm() registers its services, but the layout still needs the Klyroo.UI stylesheets in <head> and <KlyApp> around the body, as described in the Klyroo.UI installation.
  • <KlyFormProvider> in the layout if you open forms as overlays with FormService (or DynamicFormService in the runtime mode).
  • A browser with the native popover API, which Klyroo.UI uses: Chrome and Edge 114, Safari 17 or Firefox 125, or later. In a .NET MAUI app, that means iOS or Mac Catalyst 17, or an Android System WebView 114 or later.
  • Network access on the first build only if your app self-hosts web fonts (KlyrooFont), uses icons outside the built-in set, or compiles Tailwind (KlyrooTailwind): the build downloads them once and caches them under obj/klyroo. Node.js is not needed.

To build this repository you need the .NET 11 SDK (a newer SDK works too, see global.json) and a clone of klyroo-ui in the same parent folder: some projects reference it by relative path (../klyroo-ui/...). See Contributing.

Installation

Shell
dotnet add package Klyroo.Form --prerelease

For the engine alone, without Blazor, install Klyroo.Form.Core instead.

Register the services in Program.cs (or MauiProgram.cs). It also registers Klyroo.UI:

C#
using Klyroo.Form;

builder.Services.AddKlyrooForm();

Add the namespace to _Imports.razor:

Razor
@using Klyroo.Form

The runtime mode lives in its own namespace, Klyroo.Form.Dynamic: add it only where you use that mode.

Load the stylesheets in App.razor. The theme tokens come from Klyroo.UI and the form classes from this package:

Razor
<link rel="stylesheet" href="@Assets["_content/Klyroo.UI/klyroo.preflight.css"]" />
<link rel="stylesheet" href="@Assets["_content/Klyroo.UI/klyroo.css"]" />
<link rel="stylesheet" href="@Assets["_content/Klyroo.Form/klyroo-form.css"]" />

Quick start

Headless: declare a form from a model, drive it with a controller.

C#
using Klyroo.Form;
C#
public sealed class Contact
{
    public string Name { get; set; } = "";

    public string Email { get; set; } = "";

    public Topic Topic { get; set; }

    public string? Message { get; set; }
}

public enum Topic { Sales, Support }
C#
var schema = FormSchema.For<Contact>(s =>
{
    s.FormKey = "contact";
    s.Field(c => c.Name, f => f.Required());
    s.Field(c => c.Email, f => f.Required().Rule(e => e.Contains('@'), "Enter a valid email address"));
});
var name = schema.Field(c => c.Name);
var email = schema.Field(c => c.Email);
var form = new FormController<Contact>(schema);

form.Set(name, "Alice", markTouched: true);
form.Set(email, "alice", markTouched: true);
Console.WriteLine(form.GetError(email));      // Enter a valid email address
Console.WriteLine(await form.SubmitAsync());  // False

form.Set(email, "alice@example.com");
Console.WriteLine(await form.SubmitAsync((contact, ct) =>
{
    Console.WriteLine($"Send a reply to {contact.Email}");
    return Task.FromResult(true);
}));                                          // True
Console.WriteLine(form.IsDirty);              // False

Blazor: render the same schema with KlyFormEngine<TModel>. It infers TModel from Schema and Model, and the form writes the values into _contact when it submits.

Razor
@page "/contact"
@rendermode InteractiveServer
@inject IContactApi Api

<KlyFormEngine TModel="Contact" Schema="_schema" Model="_contact" SubmitLabel="Send" OnSave="SaveAsync" OnSubmit="Sent" />

@if (_sentTo is not null)
{
    <p>Thanks, we will write to @_sentTo.</p>
}

@code {
    private readonly Contact _contact = new();

    private string? _sentTo;

    // Keep the schema in a field: a new instance makes the component start a new controller.
    private static readonly FormSchema<Contact> _schema = FormSchema.For<Contact>(s =>
    {
        s.Title = "Contact us";
        s.Field(c => c.Name, f => f.Required());
        s.Field(c => c.Email, f => f.Required().Rule(e => e.Contains('@'), "Enter a valid email address"));
        s.Field(c => c.Topic);
        s.Field(c => c.Message, f => f.Required().AsTextarea().MaxLength(500));
    });

    private async Task<bool> SaveAsync(Contact contact, CancellationToken ct) => await Api.SendAsync(contact, ct);

    private void Sent(Contact contact) => _sentTo = contact.Email;
}

Api stands for your own service. Keep the schema in a field or a static member. KlyFormEngine<TModel> creates a new controller whenever it receives a different FormSchema<TModel> instance, which discards what the user typed. TModel="Contact" is written out because Razor cannot infer it when OnSubmit is a method group. A form that is not known until runtime is declared with the runtime mode instead: see Forms defined at runtime.

Documentation

Getting started Step-by-step first integration
Guides Task-oriented recipes with the typed API
Forms defined at runtime The runtime mode, for forms stored as data
Concepts How the library works
Platforms Render modes, .NET MAUI, NativeAOT
API reference Every public type and member
FAQ Troubleshooting and limitations

Compatibility

  • .NET 11 SDK.
  • Klyroo.Form.Core targets net11.0, has no dependencies, is marked trimmable and AOT compatible, and a console app that references it publishes and runs with PublishAot.
  • Klyroo.Form works in Interactive Server, Interactive WebAssembly and Interactive Auto render modes. In Static SSR the markup renders but the controls are not interactive.
  • Klyroo.Form declares browser, ios, android, maccatalyst and windows as supported platforms for .NET MAUI Blazor Hybrid. The test suite runs in bUnit, not on devices.
  • Klyroo.Form is marked trimmable and does not declare NativeAOT compatibility.

Contributing

Contributions are welcome. Read CONTRIBUTING.md before opening a pull request, and follow the code of conduct. Report security issues privately as described in SECURITY.md.

License

MIT