The samples in this folder showcase how to use remote validation on the server to prevent invalid user input. This is useful when validation conditions cannot be implemented through standard DataAnnotation
validation.
In a real situation, server validation may fail for multiple reasons:
- invalid user input
- general server error
- server unavailable
- disconnected client
- failing HTTP requests to your WebAPI
In such cases, you should notify the user that the operation failed and suggest appropriate actions.
In this sample, the WebAPI backend does not allow the user to leave the Description field empty if Defense classification is selected. In the other cases, the Description field is not required.
The example shows how you can throw exceptions in the WebAPI backend with meaningful information, send instructions/errors to the client where they can bubble up and be shown to the user.
The core concept is to return a status code higher than 400 to indicate an error, and to ensure the messages are meaningful to the human that will read them.
You can read more about the general concepts of handling WebAPI errors in articles such as:
Of course, a similar approach can be used in a server-side Blazor app. The example's purpose is to show one way to fill the input in the form with real data and to prevent data submission when errors occur.