Building Forms With Validation
Exploring Form Elements
HTML includes a variety
of form elements that are fully usable in Blazor. Ultimately, Blazor generates
HTML as output. Additionally, Blazor provides components that enhance
functionality, making it beneficial to utilize these components rather than
relying solely on HTML elements. This approach offers us excellent
functionality at no additional cost. Blazor offers the following components:
- EditForm
- InputBase < >
- InputCheckbox
- InputDate <TValue>
- InputNumber <TValue>
- InputSelect <TValue>
- InputText
- InputTextArea
- InputRadio
- InputRadioGroup
- ValidationMessage
- ValidationSummary
EditForm
EditForm
functions as a form tag but offers additional functionalities. It generates an EditContext
instance as a cascading value, allowing all components within EditForm
to share the same EditContext.
EditContext
monitors the metadata related to the editing process, including which fields
have been modified, and keeps track of any validation messages. You must either
provide a model (a class intended for editing) or an EditContext
instance. For most scenarios, it is preferable to assign a model, but in more
complex situations, you might want to initiate the EditContext manually.
EditForm has the following events that can be used to handle form
submissions:
OnValidSubmit
gets triggered when the data in the form validates correctly.
OnInvalidSubmit
gets triggered if the form does not validate correctly.
OnSubmit
gets triggered when the form is submitted, regardless of whether the form
validates correctly or not. Use OnSubmit if you want to control the
validation yourself.
InputBase < >
All the input classes in
Blazor are based on the InputBase class. This class provides a variety
of features that can be utilized across all input components. It manages AdditionalAttributes,
implying that when we include additional attributes to the tag, they will be
automatically applied to the output. Consequently, the components that inherit
from this class can utilize any HTML attributes, as they will be included in
the final output.
InputBase
includes a Value property that we can bind to, along with an event callback
named ValueChanged that triggers when the value is modified. We can also
update the DisplayName to ensure that the automated validation messages
accurately represent the intended name instead of defaulting to the property
name. It is important to note that not all controls support the DisplayName property,
and some properties are exclusively utilized within the component itself.
InputCheckbox
The InputCheckbox
component will render as <input type= “checkbox”>.
InputDate <TValue>
The InputDate
component will display as an <input type="date">. We can
utilize both DateTime and DateTimeOffset as the values for the InputDate
component. There is no option to format the date; it will adhere to the
browser's current configuration. This functionality is intentional and aligns
with the HTML5 specifications.
InputNumber
<TValue>
The InputNumber
component will render as <input type= “number”>. We can use Int32,
Int64, Single, Double, and Decimal as values for the InputNumber
component.
InputSelect <TValue>
The InputSelect
component will render as <select>.
InputText
The InputText component
will render as <input type= “text”.
InputTextArea
The InputSelect component
will render as <textarea>.
InputRadio
The InputRadio
component will render as <input type= “radio”>.
InputRadioGroup
The InputRadioGroup
component will render as <Input type= “radio”>.
ValidationMessage
The ValidationMessage
component is capable of displaying specific error messages related to
individual properties. Our goal is to utilize this component to present
validation errors beneath a form element. To add a ValidationMessage
component, we have to specify the For property with the name of the property we
want to show the validation errors for.
ValidationSummary
The ValidationSummary
component will show all the validation errors as a list for the entire EditContext.
To make sure our input controls match the Bootstrap theme (or whatever theme we
might be using), we can create our own Custom validation class.
Conclusion
All the components of
Blazor are discussed above.
Comments
Post a Comment