29. Using Model Validation Flashcards
What is Model Validation?
Model validation is the process of ensuring that the data provided in a request is valid for use in the application.
Model validation is the process of ensuring the data received by the application is suitable for binding to the model and, when this is not the case, providing useful information to the user that will help explain the problem.
Why is Model Validation useful?
Users do not always enter valid data, and using it in the application can produce unexpected and undesirable errors.
The first part of the process, checking the data received, is one of the most important ways to preserve the integrity of an application’s data. Rejecting data that cannot be used can prevent odd and unwanted states from arising in the application. The second part of the validation process is helping the user correct the problem and is equally important. Without the feedback needed to correct the problem, users become frustrated and confused.
How is Model Validation used?
Controllers and Razor Pages check the outcome of the validation process, and tag helpers are used to include validation feedback in views displayed to the user. Validation can be performed automatically during the model binding process and can be supplemented with custom validation.
Are there any pitfalls or limitations to Model Validation?
It is important to test the efficacy of your validation code to ensure that it covers the full range of values that the application can receive.
Are there any alternatives to Model Validation?
Model validation is optional, but it is a good idea to use it whenever using model binding.
Why is model validation so important?
Model validation is the process of enforcing the requirements that an application has for the data it receives from clients. Without validation, an application will try to operate on any data it receives, which can lead to exceptions and unexpected behavior that appear immediately or long-term problems that appear gradually as the database is populated with bad, incomplete, or malicious data.
What is the most direct way of validating data?
in an action or handler method,
What is the ModelStateDictionary class?
A dictionary used to track details of the state of the model object, with an emphasis on validation errors.
What are the 4 most important ModelStateDictionary members? And give a brief description.
AddModelError(property, message)
This method is used to record a model validation error for the specified property.
GetValidationState(property)
This method is used to determine whether there are model validation errors for a specific property, expressed as a value from the ModelValidationState enumeration.
getValidationState method is used to see whether there have been any errors recorded for a model property, either from the model binding process or because the AddModelError method has been called during explicit validation in the action method. The GetValidationState method returns a value from the ModelValidationState enumeration, which defines the values described in Table 29-4.
IsValid
This property returns true if all the model properties are valid and returns false otherwise.
Clear()
This property clears the validation state.
What are the 4 ModelValidationState Values and what do they mean?
Unvalidated
This value means that no validation has been performed on the model property, usually because there was no value in the request that corresponded to the property name.
Valid
This value means that the request value associated with the property is valid.
Invalid
This value means that the request value associated with the property is invalid and should not be used.
Skipped
This value means that the model property has not been processed, which usually means that there have been so many validation errors that there is no point continuing to perform validation checks.
Consider how the Name property was validated.
…
if (string.IsNullOrEmpty(product.Name)) {
ModelState.AddModelError(nameof(Product.Name), “Enter a name”);
}
…
One of the validation requirements for the Product class is to ensure the user provides a value for the Name property, so I use the static string.IsNullOrEmpty method to test the property value that the model binding process has extracted from the request. If the Name property is null or an empty string, then I know that the value cannot be used by the application, and I use the ModelState.AddModelError method to register a validation error, specifying the name of the property (Name) and a message that will be displayed to the user to explain the nature of the problem (Enter a name).
Is the ModelStateDictionary is also used during the model binding process?
The ModelStateDictionary is also used during the model binding process to record any problems with finding and assigning values to model properties.
Consider how the Price property was validated.
…
if (ModelState.GetValidationState(nameof(Product.Price))
== ModelValidationState.Valid && product.Price < 1) {
ModelState.AddModelError(nameof(Product.Price), “Enter a positive price”);
}
…
For the Price property, I check to see whether the model binding process has reported a problem parsing the value sent by the browser into a decimal value.
I want to make sure that the user provides a Price value that is equal to or greater than 1, but there is no point in recording an error about zero or negative values if the user has provided a value that the model binder cannot convert into a decimal value. I use the GetValidationState method to determine the validation status of the Price property before performing my own validation check.
Consider the following code? ... if (ModelState.IsValid) { TempData["name"] = product.Name; TempData["price"] = product.Price.ToString(); TempData["categoryId"] = product.CategoryId.ToString(); TempData["supplierId"] = product.SupplierId.ToString(); return RedirectToAction(nameof(Results)); } else { return View("Form"); } ...
After I have validated all the properties in the Product object, I check the ModelState.IsValid property to see whether there were errors. This method returns true if the Model.State.AddModelError method was called during the checks or if the model binder had any problems creating the object.
The Product object is valid if the IsValid property returns true, in which case the action method redirects the browser to the Results action, where the validated form values will be displayed. There is a validation problem if the IsValue property returns false, which is dealt with by calling the View method to render the Form view again.
What happens if there were any errors?
If the IsValue property returns false, it will be dealt with by calling the View method to render the Form view again.
It may seem odd to deal with a validation error by calling the View method, but the context data provided to the view contains details of the model validation errors; these details are used by the tag helper to transform the input elements.