Class TwForm
- Namespace
- TwBlazor.Components
- Assembly
- TwBlazor.dll
A form that validates the TwBlazor fields inside it: the model's data annotations, plus each field's own
Validator, when it is submitted.
public class TwForm : TwBlazorComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent
- Inheritance
-
TwForm
- Implements
- Inherited Members
Remarks
Bind each field to the model with @bind-Value (or the field's own value parameter, such as
@bind-SelectedValue). The field then shows the form's validation message for its property, is marked
invalid for assistive technology, and is checked again whenever it changes.
OnValidSubmit only runs once every field is valid. After a failed submit, focus moves to the error summary when ShowSummary is set, otherwise to the first invalid field, so keyboard and screen reader users land on the problem. TwBlazor fields also work in a plain EditForm.
Constructors
TwForm()
public TwForm()
Properties
ChildContent
Gets or sets the form's fields and buttons. The edit context is passed in as context.
[Parameter]
public RenderFragment<EditContext>? ChildContent { get; set; }
Property Value
CurrentEditContext
Gets the edit context the form is using.
public EditContext CurrentEditContext { get; }
Property Value
DataAnnotationsValidation
Gets or sets whether the model's data annotations, such as [Required], are checked. Defaults to
true.
[Parameter]
public bool DataAnnotationsValidation { get; set; }
Property Value
EditContext
Gets or sets the edit context the form uses, for full control over validation. Set this or Model, not both.
[Parameter]
public EditContext? EditContext { get; set; }
Property Value
Enhance
Gets or sets whether a statically rendered form is posted with enhanced navigation.
[Parameter]
public bool Enhance { get; set; }
Property Value
FocusOnError
Gets or sets whether focus moves to the error summary, or to the first invalid field, after a failed submit. Defaults to true.
[Parameter]
public bool FocusOnError { get; set; }
Property Value
FormName
Gets or sets the form's name, which a statically rendered form needs so its posted values can be found.
[Parameter]
public string? FormName { get; set; }
Property Value
HasValidated
Gets whether the form has been submitted or validated at least once.
public bool HasValidated { get; }
Property Value
IsModified
Gets whether any field has changed since the form was created.
public bool IsModified { get; }
Property Value
IsValid
Gets whether the last validation found every field valid. false until the form has been validated.
public bool IsValid { get; }
Property Value
Model
Gets or sets the object the form edits. Set this or EditContext, not both.
[Parameter]
public object? Model { get; set; }
Property Value
NativeValidation
Gets or sets whether the browser's own validation runs as well. Off by default, so the browser's error bubbles do not stop the form showing its own accessible messages.
[Parameter]
public bool NativeValidation { get; set; }
Property Value
OnInvalidSubmit
Raised when the form is submitted and at least one field is invalid.
[Parameter]
public EventCallback<EditContext> OnInvalidSubmit { get; set; }
Property Value
OnSubmit
Raised when the form is submitted, after it has been validated, whether or not it is valid.
[Parameter]
public EventCallback<EditContext> OnSubmit { get; set; }
Property Value
OnValidSubmit
Raised when the form is submitted and every field is valid.
[Parameter]
public EventCallback<EditContext> OnValidSubmit { get; set; }
Property Value
ShowSummary
Gets or sets whether a summary of every error is shown above the fields after a failed submit. Each error links to its field.
[Parameter]
public bool ShowSummary { get; set; }
Property Value
Methods
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
OnAfterRenderAsync(bool)
Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.
This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.
Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRenderboolSet to
trueif this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwisefalse.
Returns
Remarks
The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods
are useful for performing interop, or interacting with values received from @ref.
Use the firstRender parameter to ensure that initialization work is only performed
once.
OnParametersSet()
Invoked when the component's parameters have been set. Ensures that the component has a unique identifier by generating one if none is provided.
protected override void OnParametersSet()
Remarks
If the component's identifier is not set, a unique ID is generated based on the component's type name (with a 'Tw' prefix removed if present) and a GUID. This helps ensure that each component instance can be uniquely identified, which is useful for scenarios such as associating labels with form elements or targeting elements in client-side scripts.
ValidateAsync()
Validates the model and every field in the form, running each field's own validator too.
public Task<bool> ValidateAsync()