Class MudForm
- Namespace
- MudBlazor
- Assembly
- MudBlazor.dll
A component for collecting and validating user input. Every input derived from MudFormComponent within it is monitored and validated.
public class MudForm : MudComponentBase, IComponent, IHandleEvent, IHandleAfterRender, IMudStateHasChanged, IDisposable, IForm
- Inheritance
-
MudForm
- Implements
- Inherited Members
- Extension Methods
Constructors
MudForm()
public MudForm()
Fields
_errors
protected HashSet<string> _errors
Field Value
_formControls
protected HashSet<IFormComponent> _formControls
Field Value
Properties
ChildContent
The content within this form.
[Parameter]
[Category("Validated data")]
public RenderFragment? ChildContent { get; set; }
Property Value
ChildForms
protected HashSet<MudForm> ChildForms { get; }
Property Value
Classname
protected string Classname { get; }
Property Value
Disabled
Prevents the user from interacting with this form.
[Parameter]
[Category("Behavior")]
public bool Disabled { get; set; }
Property Value
Remarks
Defaults to false
.
Errors
The validation errors for inputs within this form.
[Parameter]
[Category("Validation result")]
public string[] Errors { get; set; }
Property Value
- string[]
Remarks
When this property changes, ErrorsChanged occurs.
ErrorsChanged
Occurs when Errors has changed.
[Parameter]
public EventCallback<string[]> ErrorsChanged { get; set; }
Property Value
FieldChanged
Occurs when an IFormComponent within this form has changed.
[Parameter]
public EventCallback<FormFieldChangedEventArgs> FieldChanged { get; set; }
Property Value
IsTouched
Whether any input's value has changed.
[Parameter]
[Category("Behavior")]
public bool IsTouched { get; set; }
Property Value
Remarks
When true
, an input has changed in this form or any child forms. Becomes false
when input values have been reset. When this value changes, IsTouchedChanged occurs.
IsTouchedChanged
Occurs when IsTouched has changed.
[Parameter]
public EventCallback<bool> IsTouchedChanged { get; set; }
Property Value
IsValid
Whether all inputs and child forms passed validation.
[Parameter]
[Category("Validation result")]
public bool IsValid { get; set; }
Property Value
Remarks
Defaults to true
. When this value changes, IsValidChanged occurs.
IsValidChanged
Occurs when IsValid has changed.
[Parameter]
public EventCallback<bool> IsValidChanged { get; set; }
Property Value
Model
The model populated by this form.
[Parameter]
[Category("Validated data")]
public object? Model { get; set; }
Property Value
Remarks
Properties of this model are typically linked to form input components via their For.
OverrideFieldValidation
Overrides input validation with the function or attribute in Validation.
[Parameter]
[Category("Validation")]
public bool? OverrideFieldValidation { get; set; }
Property Value
- bool?
ReadOnly
Prevents the user from changing any inputs.
[Parameter]
[Category("Behavior")]
public bool ReadOnly { get; set; }
Property Value
Remarks
Defaults to false
.
Spacing
The amount of spacing between input components, in increments of 4px
.
[Parameter]
[Category("Behavior")]
public int Spacing { get; set; }
Property Value
Remarks
Defaults to 0
. A spacing of 1
means 4px
, 2
means 8px
, and so on.
SuppressImplicitSubmission
Prevents this form from being submitted when Enter
is pressed.
[Parameter]
[Category("Behavior")]
public bool SuppressImplicitSubmission { get; set; }
Property Value
Remarks
Defaults to true
. When false
, the form will submit when Enter
is pressed, and any parent dialog will close. See:
Implicit Form Submission.
SuppressRenderingOnValidation
Prevents child components from rendering when IsValid changes.
[Parameter]
[Category("Behavior")]
public bool SuppressRenderingOnValidation { get; set; }
Property Value
Remarks
Defaults to false
. When true
, rendering performance may improve for larger forms and older devices.
Validation
The default function or attribute used to validate form components which cannot validate themselves.
[Parameter]
[Category("Validation")]
public object? Validation { get; set; }
Property Value
Remarks
Supported values are:
1. A Func<T,bool>
or Func<T,Task<bool>>
function. Returns true
if valid. When false
, a standard "Invalid"
message is shown.
2. A Func<T,string>
or Func<T,Task<string>>
function. Returns null
if valid, or a string explaining the error.
3. A Func<T,IEnumerable<string>>
or Func<T,Task<IEnumerable<string>>>
function. Returns an empty list if valid, or a list of validation errors.
3. A Func<object,string,IEnumerable<string>>
or Func<object,string,Task<IEnumerable<string>>>
function. Given the form model and path to the member, returns an empty list if valid, or a list of validation errors.
4. A ValidationAttribute object.
ValidationDelay
The delay, in milliseconds, before performing validation.
[Parameter]
[Category("Behavior")]
public int ValidationDelay { get; set; }
Property Value
Remarks
Defaults to 300
(300 milliseconds). This delay can improve rendering performance for larger forms with inputs which set Immediate to true
.
Methods
BuildRenderTree(RenderTreeBuilder)
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builder
RenderTreeBuilder
Dispose()
Releases resources used by this form.
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
GetDisabledState()
protected bool GetDisabledState()
Returns
GetReadOnlyState()
protected bool GetReadOnlyState()
Returns
OnAfterRenderAsync(bool)
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRender
bool
Returns
OnEvaluateForm()
protected Task OnEvaluateForm()
Returns
OnInitialized()
protected override void OnInitialized()
ResetAsync()
Resets all form controls and resets their validation state.
public Task ResetAsync()
Returns
Remarks
Any existing value in any form input component will be cleared.
ResetTouched()
Marks all form input components as unchanged.
public void ResetTouched()
Remarks
When called, IsTouched becomes false
.
ResetValidation()
Resets the validation state of all form controls.
public void ResetValidation()
Remarks
The values in each form input component will not be changed.
ShouldRender()
protected override bool ShouldRender()
Returns
Validate()
Forces a validation of all form controls (including in child forms).
public Task Validate()
Returns
Remarks
Validation will occur even if form controls haven't changed yet.