Skip to content

Every kind of condition

A field can be shown, required, disabled or locked by the answers around it - and each of those can be written four ways: a comparison, a group of comparisons, an expression, or C#. This registration uses all of them, and ClearOnHide() empties what the user abandoned, so it never reaches the model.

Choose Speaker: a company and a talk title appear, and the talk title is required. Choose Sponsor: the company stays, the talk title goes - and its value with it, as the model on the right shows. Pick Japan or the United States and a visa letter is offered. Tick the hotel, and the nights unlock; ask for three or more and late checkout appears. Tick Payment received and the ticket locks: still readable and in the tab order, but no longer editable.
Live form
Conference registration

Coming as
Ticket

Locked once the payment is received.

What the model holds
{
  "Name": "",
  "Role": "attendee",
  "Company": null,
  "TalkTitle": null,
  "Country": null,
  "VisaLetter": false,
  "TicketType": "standard",
  "NeedsHotel": false,
  "Nights": null,
  "LateCheckout": false,
  "Paid": false
}
View source
private static readonly string[] Schengen = ["fr", "de", "nl"];

var schema = BormBuilder.For<ConferenceRegistration>()
    .Title("Conference registration")
    .Field(x => x.Name, f => f.Required())
    .Field(x => x.Role, f => f.AsRadio()
        .Options(("attendee", "Attendee"), ("speaker", "Speaker"), ("sponsor", "Sponsor")))

    // A group: shown for speakers OR sponsors.
    .Field(x => x.Company, f => f
        .VisibleWhen(BormConditionGroup.Any(
            new BormFieldCondition("Role", BormConditionOperator.Equals, "speaker"),
            new BormFieldCondition("Role", BormConditionOperator.Equals, "sponsor")))
        .ClearOnHide()
        // Rules that apply only while a condition holds: a sponsor is a company, so say which.
        .When("Role", BormConditionOperator.Equals, "sponsor", w => w
            .Required("Sponsors are companies - which one?")
            .MinLength(3)))

    // A comparison: shown, and required, for speakers only - and emptied when it goes.
    .Field(x => x.TalkTitle, f => f
        .VisibleWhen("Role", BormConditionOperator.Equals, "speaker")
        .RequiredWhen("Role", BormConditionOperator.Equals, "speaker")
        .ClearOnHide())

    .Field(x => x.Country, f => f.Options(("fr", "France"), ("de", "Germany"), ("nl", "Netherlands"),
                                          ("jp", "Japan"), ("us", "United States")))

    // C#: a predicate over the answers, naming the field it reads.
    .Field(x => x.VisaLetter, f => f
        .VisibleWhen(data => data.GetValue("Country") is string c && !Schengen.Contains(c), "Country")
        .ClearOnHide())

    // Locked once it is settled - readable and focusable, unlike a disabled field.
    .Field(x => x.TicketType, f => f.AsRadio()
        .Options(("standard", "Standard"), ("vip", "VIP"))
        .ReadOnlyWhen(nameof(ConferenceRegistration.Paid), BormConditionOperator.IsTrue))

    .Field(x => x.NeedsHotel, f => f.AsSwitch())
    // Disabled while it does not apply yet; required once it does.
    .Field(x => x.Nights, f => f
        .Range(1, 14)
        .DisabledWhen(nameof(ConferenceRegistration.NeedsHotel), BormConditionOperator.IsFalse)
        .RequiredWhen(nameof(ConferenceRegistration.NeedsHotel), BormConditionOperator.IsTrue))

    // An expression: the fields it reads are its dependencies.
    .Field(x => x.LateCheckout, f => f
        .VisibleWhen("{NeedsHotel} and {Nights} >= 3")
        .ClearOnHide())

    .Field(x => x.Paid, f => f.AsSwitch())
    .Build();

What a condition can do

MethodWhile it holds, the field is…
VisibleWhenshown. Hidden, it is not rendered, not validated and never stops a submit; with ClearOnHide() its value is emptied too.
RequiredWhenrequired, and marked so.
DisabledWhendisabled - out of the tab order and not read out. For an answer that does not apply yet.
ReadOnlyWhenread-only - still focusable, readable and copyable. For an answer that is settled; on a group or repeater it locks everything inside, rows' buttons included.
When(condition, w => …)judged by the rules configured inside - FluentValidation's .When(…).
A step's VisibleWhen(a wizard step) shown, with everything on it - see the wizard.

Four ways to write one

  • A comparison - VisibleWhen("Role", BormConditionOperator.Equals, "speaker"). Exported to JSON Schema as {"field": "Role", "op": "Equals", "value": "speaker"}.
  • A group - BormConditionGroup.Any(…) or All(…) of comparisons, nested as deep as needed. Exported as {"any": […]}.
  • An expression - VisibleWhen("{NeedsHotel} and {Nights} >= 3"). Exported as the text it was written as; see expressions.
  • C# - VisibleWhen(data => …, "Country"), a predicate over the answers, naming the fields it reads so it is evaluated again when they change. Code has no JSON form, so it is left out of an export.

A path is resolved against the object that owns the field before the root, so inside a repeater a condition means that row. A condition naming a field that does not exist is reported by definition.Validate(). The operators:

EqualityEquals, NotEquals - numbers by value, text without regard to case
OrderGreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual - numbers and dates
SetsIn, NotIn (the value is a list), Contains, NotContains (text, a multi-select's list, or a [Flags] enum)
TextStartsWith, EndsWith, Matches (a regular expression)
StateIsEmpty, IsNotEmpty, IsTrue, IsFalse - no value needed
An unhandled error has occurred. Reload 🗙