Validation modes, summary & reset
Choose when a field revalidates, independently for before and after the first submit —
the same mode/reValidateMode split React Hook Form popularised. The
summary below lists every error in schema order and links to the control that needs fixing.
The username field is debounced, so where a mode validates on change - after the first
submit, or with Before submit set to OnChange - its async availability check runs once
you pause rather than once per character; before it, OnBlur checks it as you leave the box. Try
ada or admin and watch the indicator below.
dirty: False · submitted: 0 time(s) · saved: False · errors: 0 · checking username: no · username status: Unvalidated
The three triggers
| Trigger | A field is checked |
|---|---|
OnSubmit | Only when the form is submitted, or a wizard step advances. |
OnBlur | When the user leaves it, and on submit. |
OnChange | Whenever its value is written, when the user leaves it, and on submit. The default. |
ValidationTrigger applies until the first submit and RevalidationTrigger after it, so a form
can stay quiet while it is being filled in and turn eager once the user has asked for its verdict. Either way, a field
already showing an error is rechecked as soon as it changes, so the message goes the moment it is fixed.
A text box writes its value when the user leaves it unless it says UpdateOnInput(); that is why
OnChange on an ordinary text box looks like OnBlur.
A field's synchronous rules run first and its asynchronous ones only when those found nothing: a username that
breaks its pattern is not sent to the server to ask whether it is taken (RunAsyncRulesOnInvalidFields
changes that). A lookup that is superseded - typed past, reset, or pressed Next on twice - is cancelled through
ctx.CancellationToken and its answer discarded. GetFieldState(path).Status says where a field
stands in one word: Unvalidated, Pending, Invalid or Valid.
Rules can also report a warning that does not block the submit - see expressions -
and SingleErrorPerField shows only a field's first error - see editing an existing record.
View source
@page "/validation"
@inject DemoDirectory Directory
<BormView State="_state"
ReadOnly="_readOnly"
ShowErrorSummary="true"
ShowResetButton="true"
OnValidSubmit="HandleSubmit"
SubmitText="Create account" />
@code {
private readonly NewAccount _model = new();
private BormState _state = default!;
private bool _readOnly;
protected override void OnInitialized()
{
var schema = BormBuilder.For<NewAccount>()
.Title("Create your account")
.Field(x => x.Email, f => f.Autocomplete("email"))
.Field(x => x.Username, f => f
.Required()
.Autocomplete("username")
.Trim() // fix the value; do not complain about spaces
// Debouncing writes once per pause instead of once per character — and the async rule
// runs with that write, which is the whole reason to debounce a field that calls out
// to a service. An undebounced field's async rules still wait for blur.
.UpdateOnInput(debounceMilliseconds: 400)
.MustAsync(async ctx => await Directory.IsUsernameAvailableAsync((string?)ctx.Value ?? "", ctx.CancellationToken),
"That username is taken.")
.Help("Try 'ada' or 'admin' to see the async rule fire."))
.Field(x => x.Password, f => f.Autocomplete("new-password"))
// [Compare] on the model already wires the confirm rule.
.Field(x => x.ConfirmPassword, f => f.Autocomplete("new-password"))
.Build();
_state = new BormState(schema, new BormModelDataAccessor(_model))
{
// Stay quiet until the first submit, then correct eagerly.
ValidationTrigger = BormValidationTrigger.OnBlur,
RevalidationTrigger = BormValidationTrigger.OnChange
};
_state.StateChanged += OnStateChanged;
}
// A spinner or a tick beside one box, not a whole form that declares itself busy.
private bool CheckingUsername => _state.IsValidatingField("Username");
private bool UsernameAvailable => _state.GetFieldState("Username").Status == BormFieldStatus.Valid;
}