Overview
Schema-driven forms, validation and wizards for .NET developers, with a headless engine and Blazor components.
You declare a form from a C# model with FormSchema.: fields, conditions, rules, steps and sections, written with refactor-safe expressions and checked by the compiler. A FormController 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., 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 everydotnet add packagecommand needs--prerelease. Klyroo.alone (the form controller) runs in any .NET 11 app, with no other dependency.Form. Core - A Blazor app for
Klyroo.: 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 theForm .project.Client - Klyroo.UI set up in the app.
Klyroo.brings the Klyroo.UI package, andForm 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. <Klyin the layout if you open forms as overlays withForm Provider> FormService(orDynamicin the runtime mode).Form Service - 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 underobj/. Node.js is not needed.klyroo
To build this repository you need the .NET 11 SDK (a newer SDK works too, see global.) and a clone of klyroo-ui in the same parent folder: some projects reference it by relative path (../). See Contributing.
Installation
dotnet add package Klyroo.Form --prereleaseFor the engine alone, without Blazor, install Klyroo. instead.
Register the services in Program. (or MauiProgram.). It also registers Klyroo.UI:
using Klyroo.Form;
builder.Services.AddKlyrooForm();Add the namespace to _:
@using Klyroo.FormThe runtime mode lives in its own namespace, Klyroo.: add it only where you use that mode.
Load the stylesheets in App.. The theme tokens come from Klyroo.UI and the form classes from this package:
<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.
using Klyroo.Form;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 }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); // FalseBlazor: render the same schema with KlyFormEngine. It infers TModel from Schema and Model, and the form writes the values into _ when it submits.
@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 creates a new controller whenever it receives a different FormSchema 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.targetsForm. Core net11.0, has no dependencies, is marked trimmable and AOT compatible, and a console app that references it publishes and runs withPublishAot.Klyroo.works in Interactive Server, Interactive WebAssembly and Interactive Auto render modes. In Static SSR the markup renders but the controls are not interactive.Form Klyroo.declaresForm browser,ios,android,maccatalystandwindowsas supported platforms for .NET MAUI Blazor Hybrid. The test suite runs in bUnit, not on devices.Klyroo.is marked trimmable and does not declare NativeAOT compatibility.Form
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.