Blazor's own inputs
InputText, InputNumber, ValidationSummary and the inputs of most
component libraries need a Blazor EditContext. ProvideEditContext cascades one that
tells the same story as the form, so those controls can sit in a custom renderer and show the field's errors
as their own - while the values still go through Borm.
The name, email and age below are Blazor's
InputText and InputNumber; the city is
Borm's own text box. Submit the empty form: both kinds turn invalid together, and the
ValidationSummary at the bottom of the form - a stock Blazor component - lists what Borm found.
A message reaches the edit context when the form would show it, so nothing turns red before it should.
View source
@page "/blazor-inputs"
<BormView Definition="_schema" Model="_model" ProvideEditContext="true" OnValidSubmit="HandleSubmit">
<ValidationSummary />
</BormView>
@code {
private readonly ContactCard _model = new();
private BormDefinition _schema = default!;
protected override void OnInitialized()
{
// Program.cs: services.AddBorm(r => r.RegisterCustom<BlazorTextInput>("blazor-text")
// .RegisterCustom<BlazorNumberInput>("blazor-number"));
_schema = BormBuilder.For<ContactCard>()
.Title("Contact card")
.Field(x => x.Name, f => f.Required().CustomRenderer("blazor-text"))
.Field(x => x.Email, f => f.Required().Email().CustomRenderer("blazor-text"))
.Field(x => x.Age, f => f.Range(18, 120).CustomRenderer("blazor-number"))
.Object(x => x.Address, a => a.Field(c => c.City, f => f.Required()))
.Build();
}
}
@* Renderers/BlazorTextInput.razor *@
@inherits BormInputBase
<InputText @ref="_input"
id="@Context.ElementId"
class="@CssClass"
Value="@Context.StringValue"
ValueChanged="@((string? value) => Context.SetFromStringAsync(value))"
ValueExpression="@(Context.ValueExpression<string?>())"
disabled="@Disabled"
aria-describedby="@DescribedBy" />
@code {
private InputText? _input;
// A component is no element: hand the form the one InputText renders, so it can be focused.
public override async ValueTask<bool> FocusAsync()
{
if (_input?.Element is not { } element) return false;
await element.FocusAsync();
return true;
}
}How the edit context behaves
- The form stays the source of truth. Values are written through the field context, never the
EditContext, so conditions, computed values and dirty tracking stay right.Context.ValueExpression<T>()names the property that owns the field - the model, a nested object, a list's row, or a dictionary key - so Blazor files the field's messages under the right identifier. Ask for the property's own type:ValueExpression<string>()over anintthrows rather than read it wrongly. - A message reaches the context when the form would show it - the field touched, or the form submitted.
Only errors are copied: an
EditContexthas no severity, so warnings stay Borm's. EditContext.Validate()runs the form's validation, which is what a hand-written submit button over the context does.Context.EditContextandContext.FieldIdentifierare there for a component that wants them, and null while the view provides no context. For a state rendered without a view, construct aBormEditContextAdapterover it and cascade itsEditContext.
Off by default: a form that does not use these controls has no cascade in its tree.