Array / repeater
Array fields render a repeating editor with add, remove and reorder. Each item is a nested object validated like any other field, and its errors travel with it when rows move. The Total below is a computed field: the schema derives it from the lines, so no page code keeps it in sync.
Add a few lines and type quantities and prices: each line's total and the invoice total follow. Move a line up or
down, or remove it - its values, its errors and whether it was touched travel with it. Try it with the keyboard:
adding a line puts the caret in it; removing one moves the caret to the line that took its place — rather than
dropping it on the page body, which is where a button that deletes itself normally leaves you. An invoice needs at
least one line (
Items(min: 1)), so the last one cannot be removed; on a repeater that can be emptied,
the caret goes to the add button.
Disabled makes the form inoperable — every control and button, the repeater's
buttons included. It is deliberately not the same as ReadOnly, which keeps the
fields focusable and readable for a review screen.
View source
@page "/array"
@implements IDisposable
<label><input type="checkbox" @bind="_locked" /> Lock the invoice</label>
@* Disabled: every control and button, the repeater's included - a save in flight, a locked record. *@
<BormView State="_state" OnValidSubmit="HandleSubmit" SubmitText="Save invoice"
ShowErrorSummary="true" Disabled="_locked" />
@code {
private readonly Invoice _model = new();
private BormState _state = default!;
private bool _locked;
protected override void OnInitialized()
{
var schema = BormBuilder.For<Invoice>()
.Title("New invoice")
.Field(x => x.Customer, f => f.Required().Autocomplete("organization"))
.Array(x => x.Lines, line => line
.Field(l => l.Description, f => f.Required())
.Field(l => l.Quantity, f => f.Range(1, 9999))
.Field(l => l.UnitPrice, f => f.Range(0, 1_000_000).Step(0.01))
// Each line's total is derived from its own quantity and price: "Quantity" is
// named relative to the row, so the one formula works on every row.
.Computed(l => l.LineTotal, l => l.Quantity * l.UnitPrice,
dependsOn: ["Quantity", "UnitPrice"]),
// The list itself: at least one row, and buttons that say "Add line" and
// "Remove line 2" rather than "Add item".
f => f.Items(min: 1, message: "Add at least one line.").Attr("itemNoun", "line"))
// ...and the invoice total from every line. Recomputed whenever anything under Lines changes.
.Computed(x => x.Total, m => m.Lines.Sum(l => l.Quantity * l.UnitPrice),
dependsOn: [nameof(Invoice.Lines)])
.Build();
_state = new BormState(schema, new BormModelDataAccessor(_model));
_state.StateChanged += OnStateChanged;
}
private void OnStateChanged() => InvokeAsync(StateHasChanged);
public void Dispose()
{
_state.StateChanged -= OnStateChanged;
// The page created the state, so the page disposes it: an options load or async rule still in
// flight is cancelled instead of finishing against a form nobody is looking at.
_state.Dispose();
}
}The other shapes of a list
- A list of plain values - with no model,
ArrayOf("phones", BormFieldType.Tel, f => f.Items(max: 3))gives each value a row of its own;UniqueItems()refuses two rows that hold the same thing. For short words, tags are the better shape. - Options on the list -
Items(min, max)bounds the row count,SeedMinItems()opens with the rows the minimum asks for,Attr("duplicable", true)adds a Duplicate button to each row,Attr("reorderable", false)takes the up and down buttons away, andPageSize(n)shows a long list a page at a time. - Rows that think for themselves - a condition, a computed value or a cascading list on the row template reads that row: see per-row conditions.
- From code -
AddArrayItem,InsertArrayItem,DuplicateArrayItem,RemoveArrayItem,MoveArrayItem,SwapArrayItemsandClearArrayItemson the state, with each row's messages and touched state following it - see editing an existing record. AT[]property works too: it is replaced with a resized copy.