Skip to content

Async & cascading options

Options do not have to be baked into the schema. OptionsFrom loads them on demand and declares which fields they depend on — pick a country and the city list reloads (dropping any selection that no longer exists). The company field below is conditionally required and clears itself when it is hidden, so an abandoned branch never reaches your model.

Open the city list before choosing a country: it is empty and says to pick one first. Choose France, then a city, then change the country to Japan - the list reloads (the lookup takes a moment, and the select says it is loading) and the French city, which is not a Japanese one, is cleared. Tick Business delivery, type a company, then untick it: the company is cleared, as the submitted JSON shows.
Live form
Where should we deliver?

View source
@page "/cascading"
@inject DemoDirectory Directory

<BormView State="_state" OnValidSubmit="HandleSubmit" SubmitText="Save address" />

@code {
    private readonly DeliveryAddress _model = new();
    private BormState _state = default!;

    protected override void OnInitialized()
    {
        var schema = BormBuilder.For<DeliveryAddress>()
            .Title("Where should we deliver?")
            .Field(x => x.Country, f => f
                .Required()
                .Options(DemoDirectory.Countries.ToArray())
                .Autocomplete("country"))
            .Field(x => x.City, f => f
                .Required()
                // Loaded on demand, and reloaded whenever Country changes.
                .OptionsFrom(
                    load: async ctx => await Directory.GetCitiesAsync(ctx.Value("Country") as string, ctx.CancellationToken),
                    value: city => city,
                    label: city => city,
                    dependsOn: nameof(DeliveryAddress.Country))
                .Placeholder("Pick a country first"))
            .Field(x => x.IsBusiness)
            .Field(x => x.CompanyName, f => f
                .VisibleWhen(nameof(DeliveryAddress.IsBusiness), BormConditionOperator.IsTrue)
                .RequiredWhen(nameof(DeliveryAddress.IsBusiness), BormConditionOperator.IsTrue)
                .ClearOnHide())
            .Field(x => x.Notes, f => f.AsTextArea(2))
            .Build();

        _state = new BormState(schema, new BormModelDataAccessor(_model));
        _state.StateChanged += OnStateChanged;
    }
}

Where choices come from

  • Options(("fr", "France"), …) - a fixed list in the schema. OptionsFromEnum<TEnum>() lists an enum's members; a property typed as an enum gets them without asking, labelled by [Display(Name)].
  • OptionsFrom(load, value, label, dependsOn) - loaded when first needed, cancelled through ctx.CancellationToken when superseded, and loaded again when a field it depends on changes. Inside a repeater dependsOn: "Country" and ctx.Value("Country") mean that row's country, and naming a container ("Address") covers everything inside it.
  • SearchFrom(search, value, label) - for a list too long to load at all: the service is asked as the user types. See lookups that search as you type.

A provider that throws is recorded, not thrown: the control says the options could not be loaded, state.OptionsError(path) holds the exception, OptionsLoadFailed announces it and InvalidateOptions(path) tries again - see resilience. Options from a provider live in the form's state rather than the schema, so they are not exported to JSON Schema, and a choice made from them should be checked again on the server, where the same lookup lives. The same options drive a type-to-filter combobox.

An unhandled error has occurred. Reload 🗙