From a C# type
One line generates a complete form from any POCO. Labels, field types, options and validation come from the type and its DataAnnotations — and fields inherited from a base class come first, in the order the class reads.
[Display(Name)] gave the labels, the AccountKind
enum became a dropdown of its members, the bool a checkbox, and [Required],
[StringLength] and [EmailAddress] the rules. Leave a field and its rules run - this form was
set to validate on blur. The model is shown in the source.
View source
// Forms/Models.cs - a plain model; nothing in it knows about Borm.
public enum AccountKind { Personal, Business }
public class AccountSignup
{
[Required, Display(Name = "Full name"), StringLength(60, MinimumLength = 2)]
public string FullName { get; set; } = "";
[Required, EmailAddress]
public string Email { get; set; } = "";
[Display(Name = "Account type")]
public AccountKind AccountKind { get; set; }
[Display(Name = "Company name")]
public string? CompanyName { get; set; }
[Display(Name = "VAT number")]
public string? VatNumber { get; set; }
[Display(Name = "I accept the terms and conditions")]
public bool AcceptTerms { get; set; }
}
@page "/quickstart"
@* ConfigureState settles the state the view builds for you, so wanting one setting does not mean
constructing, holding and disposing a BormState by hand. *@
<BormView Definition="_schema" Model="_model"
ConfigureState="s => s.ValidationTrigger = BormValidationTrigger.OnBlur"
OnValidSubmit="HandleSubmit" SubmitText="Create account" />
@code {
private readonly AccountSignup _model = new();
private BormDefinition _schema = default!;
protected override void OnInitialized()
{
// Zero-config: generate the whole form from the type.
_schema = BormSchemaGenerator.Generate<AccountSignup>();
}
private void HandleSubmit(BormState state)
{
var json = System.Text.Json.JsonSerializer.Serialize(_model,
new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
}
}What the generator reads
Attributes keep the meaning DataAnnotations gives them, so the form never accepts a value the model's own validation would then reject.
| On the model | In the form |
|---|---|
string, numbers, bool, dates and times, enums | A text box, a number box, a checkbox, the matching date or time picker, a dropdown of the enum's members (labelled by their [Display(Name)]). |
[Flags] enum, List<TEnum> | A multi-select: a set of tick boxes. |
A class, a struct | A nested group of its own fields. List<T> of a class is a repeater. |
IBrowserFile, List<IBrowserFile> | A file picker. byte[] and Stream are left out. |
[Display(Name, Description, Prompt, Order, GroupName)] | The label, the help text, the placeholder, the order, and a <fieldset> around consecutive fields sharing a group. |
[Required] | Required - on a checkbox, "must be ticked". |
[StringLength], [MinLength], [MaxLength], [Length] | Characters on text; items on a collection - a repeater, a multi-select, a list of files. |
[Range] | Numbers, dates and times, exclusive bounds honoured; also the control's min and max. |
[RegularExpression] | Must match the whole value, as DataAnnotations means it. |
[EmailAddress], [Url], [Phone], [DataType(Password)] | The matching input type and rule; [Url] accepts http, https and ftp. |
[Compare] | A "must match" rule that also revalidates when the other field changes. |
[FileExtensions] | On a file, the picker's accept list and a rule; on a string, a rule on the file name it holds. |
A private or internal setter, a get-only property | Rendered read-only. A class-typed group or list is edited in place, so a private setter on one is fine. |
BormSchemaGenerator.Generate<T>(options) takes IgnoredProperties, MaxDepth,
HonorScaffoldColumn, ReadOnlyProperties (render them read-only, or leave them out) and
ConfigureField, a hook run on every generated field for project-wide conventions. To configure the
fields one by one, the fluent builder starts from exactly this and changes only what you say;
and IValidatableObject covers the rules that read more than one property.