Adding validation
We have already touched on the subject of validation; there are some built-in functionalities in the input
components as well as EditForm
to handle validation.
One way to add validation to our form is to use DataAnnotations
. By using data annotations, we don't have to write any custom logic to make sure the data in the form is correct; we can instead add attributes to the data model and let DataAnnotationsValidator
take care of the rest.
There are a bunch of DataAnnotations
instances in .NET already that we can use; we can also build our own annotations.
Some of the built-in data annotations are as follows:
Required
: Makes the field requiredEmail
: Will check that the entered value is an email addressMaxLength
: Will check that the number of characters is not exceededRange
: Will check that the value is within a certain range
There are many more annotations that can help us validate our data. To test this out, let's add...