Custom validation class attributes
By simply using the edit form, input components, and DataAnnotationValidator
, the framework will automatically add classes to the components when it's valid and when it's not valid.
By default, these classes are .valid
and .invalid
. In .NET 5, we are given a way to customize these class names ourselves.
When using Bootstrap, the default class names are .is-valid
and .is-invalid
and the class names must also have .form-control
to get the right styles.
The component we are building next will help us to get the right Bootstrap styling on all of our form components.
We will create our own FieldCssClassProvider
to customize what classes Blazor will use:
- In the
MyBlogServerSide
project, right-click in theComponents
folder and select Add class, and name the classBootstrapFieldCssClassProvider
. - Open the new class and add the following code:
using Microsoft.AspNetCore.Components.Forms; using System.Linq; namespace MyBlogServerSide...