Class MudFormComponent<T, U>
- Namespace
- MudBlazor
- Assembly
- MudBlazor.dll
Represents a base class for designing form input components.
public abstract class MudFormComponent<T, U> : MudComponentBase, IComponent, IHandleEvent, IHandleAfterRender, IMudStateHasChanged, IFormComponent, IAsyncDisposable
Type Parameters
T
The complex type managed by this input.
U
The value type managed by this input.
- Inheritance
-
MudFormComponent<T, U>
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
MudFormComponent(Converter<T, U>)
protected MudFormComponent(Converter<T, U> converter)
Parameters
converter
Converter<T, U>
Fields
_value
This is the form component's value.
protected T? _value
Field Value
- T
Properties
ConversionError
Indicates a problem has occurred during conversion.
public bool ConversionError { get; }
Property Value
Remarks
When true
, the Converter was unable to convert values, usually due to invalid input.
ConversionErrorMessage
The error describing why type conversion failed.
public string? ConversionErrorMessage { get; }
Property Value
Remarks
When set, returns the reason that the Converter was unable to convert values, usually due to invalid input.
Converter
The type converter for this input.
[Parameter]
[Category("Behavior")]
public Converter<T, U> Converter { get; set; }
Property Value
- Converter<T, U>
Remarks
This property provides a way to customize conversions between T
objects and U
values. If no converter is specified, a default will be chosen based on the kind of input.
Culture
The culture used to format and interpret values such as dates and currency.
[Parameter]
[Category("Behavior")]
public CultureInfo Culture { get; set; }
Property Value
Remarks
Defaults to InvariantCulture.
Error
Displays an error.
[Parameter]
[Category("Validation")]
public bool Error { get; set; }
Property Value
Remarks
Defaults to false
. When true
, the text in ErrorText is displayed.
ErrorId
The ID of the error description element, for use by aria-describedby
when Error is true
.
[Parameter]
[Category("Validation")]
public string? ErrorId { get; set; }
Property Value
Remarks
Defaults to null
. When set and the Error property is true
, an aria-describedby
attribute is rendered to improve accessibility for users.
ErrorText
The text displayed if the Error property is true
.
[Parameter]
[Category("Validation")]
public string? ErrorText { get; set; }
Property Value
For
The model field containing validation attributes.
[Parameter]
[Category("Validation")]
public Expression<Func<T>>? For { get; set; }
Property Value
- Expression<Func<T>>
Remarks
When using an EditForm, this property is used to find data annotation validation attributes such as MaxLengthAttribute used to perform validation.
HasErrors
Indicates any error, conversion error, or validation error with this input.
public bool HasErrors { get; }
Property Value
Remarks
When true
, the Error property is true
, or ConversionError is true
, or one or more ValidationErrors exists.
IsForNull
Indicates whether the For property is null
.
public bool IsForNull { get; }
Property Value
Required
Requires an input value.
[Parameter]
[Category("Validation")]
public bool Required { get; set; }
Property Value
Remarks
Defaults to false
. When true
, an error with the text in RequiredError will be shown during validation if no input was given.
RequiredError
The text displayed during validation if no input was given.
[Parameter]
[Category("Validation")]
public string RequiredError { get; set; }
Property Value
Remarks
Defaults to "Required"
. This text is only shown when Required is true
.
Touched
Indicates whether the user has interacted with this input or the focus has been released.
public bool Touched { get; protected set; }
Property Value
Remarks
Defaults to false
. When true
, the user has performed input, or focus has moved away from this input. This property is typically used to show the RequiredError text only after the user has interacted with this input.
Validation
The function used to detect problems with the input.
[Parameter]
[Category("Validation")]
public object? Validation { get; set; }
Property Value
Remarks
When using a MudForm, this property can be any of several kinds of functions:
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.
ValidationErrors
The list of problems with the current input value.
public List<string> ValidationErrors { get; set; }
Property Value
Remarks
When using a MudForm, this property is updated when validation has been performed. Use the Validation property to control what validations are performed.
Methods
BeginValidateAsync()
protected Task BeginValidateAsync()
Returns
BeginValidationAfterAsync(Task)
protected Task BeginValidationAfterAsync(Task task)
Parameters
task
Task
Returns
DisposeAsync()
public ValueTask DisposeAsync()
Returns
DisposeAsyncCore()
Called to dispose this instance.
protected virtual ValueTask DisposeAsyncCore()
Returns
FieldChanged(object?)
Notify the Form that a field has changed if SubscribeToParentForm is true
protected void FieldChanged(object? newValue)
Parameters
newValue
object
GetErrorText()
The current error or conversion error.
public string? GetErrorText()
Returns
- string
This property returns the value in ErrorText or ConversionErrorMessage.
HasValue(T?)
protected virtual bool HasValue(T? value)
Parameters
value
T
Returns
OnConversionErrorOccurred(string)
protected virtual void OnConversionErrorOccurred(string error)
Parameters
error
string
OnInitializedAsync()
protected override Task OnInitializedAsync()
Returns
OnParametersSet()
protected override void OnParametersSet()
ReadValue()
protected virtual T? ReadValue()
Returns
- T
RegisterAsFormComponent()
protected virtual void RegisterAsFormComponent()
ResetAsync()
Clears the input and any validation errors.
public Task ResetAsync()
Returns
Remarks
When called, the Value
, Error, ErrorText, and ValidationErrors properties are all reset.
ResetValidation()
Clears any validation errors.
public void ResetValidation()
Remarks
When called, the Error, ErrorText, and ValidationErrors properties are all reset.
ResetValueAsync()
protected virtual Task ResetValueAsync()
Returns
SetConverter(Converter<T, U>)
protected virtual bool SetConverter(Converter<T, U> value)
Parameters
value
Converter<T, U>
Returns
SetCulture(CultureInfo)
protected virtual bool SetCulture(CultureInfo value)
Parameters
value
CultureInfo
Returns
Validate()
Causes validation to be performed for this input.
public Task Validate()
Returns
Remarks
When using a MudForm, the input is validated via the function set in the Validation property.
ValidateModelWithFullPathOfMember(Func<object, string, IEnumerable<string?>>, List<string>)
protected virtual void ValidateModelWithFullPathOfMember(Func<object, string, IEnumerable<string?>> func, List<string> errors)
Parameters
ValidateModelWithFullPathOfMember(Func<object, string, Task<IEnumerable<string?>>>, List<string>)
protected virtual Task ValidateModelWithFullPathOfMember(Func<object, string, Task<IEnumerable<string?>>> func, List<string> errors)
Parameters
Returns
ValidateValue()
protected virtual Task ValidateValue()
Returns
ValidateWithAttribute(ValidationAttribute, T?, List<string>)
protected virtual void ValidateWithAttribute(ValidationAttribute attr, T? value, List<string> errors)
Parameters
attr
ValidationAttributevalue
Terrors
List<string>
ValidateWithFunc(Func<T?, bool>, T?, List<string>)
protected virtual void ValidateWithFunc(Func<T?, bool> func, T? value, List<string> errors)
Parameters
ValidateWithFunc(Func<T?, IEnumerable<string?>>, T?, List<string>)
protected virtual void ValidateWithFunc(Func<T?, IEnumerable<string?>> func, T? value, List<string> errors)
Parameters
func
Func<T, IEnumerable<string>>value
Terrors
List<string>
ValidateWithFunc(Func<T?, string?>, T?, List<string>)
protected virtual void ValidateWithFunc(Func<T?, string?> func, T? value, List<string> errors)
Parameters
ValidateWithFunc(Func<T?, Task<bool>>, T?, List<string>)
protected virtual Task ValidateWithFunc(Func<T?, Task<bool>> func, T? value, List<string> errors)
Parameters
Returns
ValidateWithFunc(Func<T?, Task<IEnumerable<string?>>>, T?, List<string>)
protected virtual Task ValidateWithFunc(Func<T?, Task<IEnumerable<string?>>> func, T? value, List<string> errors)
Parameters
Returns
ValidateWithFunc(Func<T?, Task<string?>>, T?, List<string>)
protected virtual Task ValidateWithFunc(Func<T?, Task<string?>> func, T? value, List<string> errors)
Parameters
Returns
WriteValueAsync(T?)
protected virtual Task WriteValueAsync(T? value)
Parameters
value
T