Fluent builder + conditions
BormBuilder.For<T>() configures each field where it is named, with the property's own type:
x => x.Email is checked by the compiler, so renaming the property renames the field. It starts from
what the model's attributes already say and changes only what you tell it to. The company fields below appear
only when the account type is Business.
View source
@page "/builder"
<BormView Definition="_schema" Model="_model" OnValidSubmit="HandleSubmit" SubmitText="Sign up" />
@code {
private readonly AccountSignup _model = new();
private BormDefinition _schema = default!;
protected override void OnInitialized()
{
_schema = BormBuilder.For<AccountSignup>()
.Title("Create your account")
.Field(x => x.FullName, f => f.Required().MinLength(2).Placeholder("Ada Lovelace"))
.Field(x => x.Email, f => f.Required().Email().Placeholder("[email protected]"))
.Field(x => x.AccountKind, f => f.AsRadio().Label("Account type"))
.Field(x => x.CompanyName, f => f
.Label("Company name")
.Required("Company name is required for business accounts.")
.VisibleWhen("AccountKind", BormConditionOperator.Equals, "Business"))
.Field(x => x.VatNumber, f => f
.Label("VAT number")
.Pattern(@"^[A-Z]{2}\d{8,12}$", "Use a format like GB123456789.")
.VisibleWhen("AccountKind", BormConditionOperator.Equals, "Business"))
.Field(x => x.AcceptTerms, f => f
.Label("I accept the terms and conditions")
.Required("You must accept the terms to continue."))
.Build();
}
private void HandleSubmit(BormState state)
{
var json = System.Text.Json.JsonSerializer.Serialize(_model,
new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
}
}Try it
Submit the empty form: the three required fields say so, and focus goes to the first. Choose Business:
the company name and VAT number appear, and the company name is now required. Choose Personal again and
they go - with their rules, so they no longer stop the submit. The terms box is a checkbox with
Required(), which on a checkbox means "must be ticked".
Nested members, groups and lists
Every path is an expression. A nested member binds to its dotted path; Object and Array
configure a group or a repeater with a builder of their own, typed to the child.
BormBuilder.For<Order>()
.Field(x => x.Customer.Email, f => f.Required()) // binds to "Customer.Email"
.Object(x => x.ShippingAddress, a => a
.Field(p => p.Street)
.Field(p => p.City))
.Array(x => x.Lines, line => line
.Field(l => l.Product, f => f.Required())
.Field(l => l.Quantity, f => f.Range(1, 999)))
.Build();
A constraint set in the builder replaces the rule the same attribute generated, so restating
[Required] - as the full name above does, over the model's own [Required] - produces one
error, not two.
BormBuilder<T>.Path(x => x.Customer.Email) gives the path string for a condition or a step.
With no model at all
BormBuilder.Create() builds the same schema by name, for a form whose shape is not a compiled type. It
binds to a dictionary, passed as Data. The field types page is built this way.
var definition = BormBuilder.Create() .Title("Feedback") .Text("subject", f => f.Required()) .TextArea("body", f => f.MaxLength(2000)) .Select("topic", f => f.Options(("bug", "Bug"), ("idea", "Idea"))) .ArrayOf("tags", BormFieldType.Text, f => f.Items(max: 5).UniqueItems()) .Build(); <BormView Definition="definition" Data="_data" OnValidSubmit="Send" /> private readonly Dictionary<string, object?> _data = new();
Showing and hiding is one of four conditions - VisibleWhen, RequiredWhen,
DisabledWhen and ReadOnlyWhen - each written as a comparison, a group, an expression or
C#; every kind of condition shows them side by side.