FormFields
Draws one field of a typed form where you want it (KlyFormFieldView) or replaces the control of a field with your own markup (KlyFieldTemplate), while the library still draws the label, description, help and error.
On this page
Examples
Your own layout
The page owns the controller and places each field in a grid. KlyFormFieldView does not subscribe to the controller, so the page re-renders on Changed, which KlyFormEngine does for its own fields. TModel and TValue are inferred from the handle the schema gave.
@implements IDisposable
@* The form component is not used: this page owns the controller, places each field itself and re-renders when the controller changes. *@
<CascadingValue Value="_form">
<div class="flex w-full max-w-2xl flex-col gap-4">
<div class="grid gap-4 sm:grid-cols-2">
<KlyFormFieldView Field="_name" />
<KlyFormFieldView Field="_email" />
</div>
<KlyFormFieldView Field="_message" />
<div class="flex items-center gap-3">
<KlyButton Label="Send" OnClick="SendAsync" />
<span class="text-sm text-(--klyroo-text-muted)">@_status</span>
</div>
</div>
</CascadingValue>
@code {
private static readonly FormSchema<Message> Schema = FormSchema.For<Message>(s =>
{
s.FormKey = "message";
s.Field(m => m.Name, f => f.Label("Name").Required());
s.Field(m => m.Email, f => f.Label("Email").Required().Rule(v => v.Contains('@'), "Enter a valid email address"));
s.Field(m => m.Text, f => f.Label("Message").AsTextarea().Required().MaxLength(300));
});
private readonly FormController<Message> _form = new(Schema);
private readonly FormField<Message, string> _name = Schema.Field(m => m.Name);
private readonly FormField<Message, string> _email = Schema.Field(m => m.Email);
private readonly FormField<Message, string> _message = Schema.Field(m => m.Text);
private string _status = "";
protected override void OnInitialized() => _form.Changed += OnChanged;
private void OnChanged() => InvokeAsync(StateHasChanged);
private async Task SendAsync() => _status = await _form.SubmitAsync() ? "Sent" : "Fix the errors above";
public void Dispose() => _form.Changed -= OnChanged;
public sealed class Message
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public string Text { get; set; } = "";
}
}Custom control
A KlyFieldTemplate inside the form names its field by handle and reads a FormControlContext typed as the member (Id, Value, Current, Set, Commit, Blur, Error, Disabled). Commit is for a change that is complete in one gesture, Set for an edit in progress. A template replaces the control of any field, a built-in kind as well as one declared with AsCustom(), and it does not apply to the columns of a repeatable list.
<div class="w-full max-w-lg">
<KlyFormEngine TModel="Review" Schema="Schema" Model="_review" SubmitLabel="Post review">
<KlyFieldTemplate Field="_rating" Context="c">
<div id="@c.Id" role="radiogroup" aria-labelledby="@c.LabelledBy" aria-describedby="@c.DescribedBy" aria-invalid="@(c.Error is null ? null : "true")" class="flex gap-1">
@for (var star = 1; star <= 5; star++)
{
var value = star;
<button type="button" disabled="@c.Disabled" aria-pressed="@(c.Current() >= value ? "true" : "false")" aria-label="@($"{value} out of 5")"
class="cursor-pointer border-0 bg-transparent p-0 text-2xl @(c.Current() >= value ? "text-(--klyroo-primary)" : "text-(--klyroo-text-muted)")"
@onclick="@(() => c.Commit(value))" @onblur="c.Blur">★</button>
}
</div>
</KlyFieldTemplate>
<KlyFieldTemplate Field="_volume" Context="c">
<input type="range" min="0" max="11" id="@c.Id" value="@c.Value" aria-describedby="@c.DescribedBy"
@oninput="@(e => c.Set(int.Parse(e.Value!.ToString()!)))" @onblur="c.Blur" />
</KlyFieldTemplate>
</KlyFormEngine>
</div>
@code {
private readonly Review _review = new();
private static readonly FormSchema<Review> Schema = FormSchema.For<Review>(s =>
{
s.FormKey = "review";
s.Field(m => m.Rating, f => f.Range(1, 5).Required().Label("How was it?"));
s.Field(m => m.Volume, f => f.Range(0, 11).Label("Volume"));
s.Field(m => m.Comment, f => f.AsTextarea());
});
// The handles tell each template which field it draws.
private readonly FormField<Review, int> _rating = Schema.Field(m => m.Rating);
private readonly FormField<Review, int> _volume = Schema.Field(m => m.Volume);
public sealed class Review
{
public int Rating { get; set; }
public int Volume { get; set; }
public string Comment { get; set; } = "";
}
}API
KlyFormFieldView<TModel>
Renders one field of a typed form with the control of its kind, its label, description, hint, help, error and per-field reset, the way KlyFormEngine<TModel> and KlyFormPage<TModel> render their fields, for a layout of your own. Containers, tabs and repeatable lists render their children too. Place it in a CSS grid, since the field sets its own grid-column span. It renders nothing when the field is not visible ( FormItem{`0}) ). It does not subscribe to Changed : render it from a component that re-renders on that event ( KlyFormEngine does), so changes made elsewhere show up.
Parameters
| Name | Type | Description |
|---|---|---|
Class |
string |
Classes added to the root element, merged last so they win over the theme. |
Field (required) |
FormItem |
The field or layout element to render: the handle the schema gave. is inferred from it. |
Form |
FormController |
The controller that holds the value of the field. When it is not set, the one of the enclosing KlyFormEngine or KlyFormPage is used, or the one of a CascadingValue . |
KlyFieldTemplate<TModel, TValue>
Draws the control of one field of a typed form with your markup. Put it inside KlyFormEngine<TModel> (or KlyFormPage<TModel> ), name the field with its handle, and read the FormControlContext`1 it gives ( Value , Set , Commit , Id , Error , Disabled , Blur ): the component still draws the label, description, help and error around your control. The template replaces the control of any field, so a field you declared with AsCustom() needs one, and a built-in kind can be swapped for your own. and are inferred from Field . It renders nothing itself.
Parameters
| Name | Type | Description |
|---|---|---|
Field (required) |
FormField |
The field whose control this template draws: the handle the schema gave. and are inferred from it. |
Form |
FormController |
The controller of the form. When it is not set, the one of the enclosing KlyFormEngine or KlyFormPage is used. |
Slots
| Name | Type | Description |
|---|---|---|
ChildContent (required) |
RenderFragment |
Draws the control from the context of the field: its value typed as the member of the model, how to change it, and the ids and state the control needs. |