React to feedback
parent
11d5aacbda
commit
d060967835
|
@ -47,7 +47,7 @@ Simply reading through the model reveals the rules about data for this app, maki
|
|||
|
||||
* `[RegularExpression]`: Validates that the data matches the specified regular expression.
|
||||
|
||||
* `[Required]`: Makes a property required. Non-nullable [value types](/dotnet/csharp/language-reference/keywords/value-types) (such as `decimal`, `int`, `float`, and `DateTime`) are inherently required and don't need the `Required` attribute. If you create a [nullable type](/dotnet/csharp/programming-guide/nullable-types/) (for example `decimal?`), you can provide a functional `Required` attribute.
|
||||
* `[Required]`: Makes a property required.
|
||||
|
||||
* `[StringLength]`: Validates that a string property has at most the given maximum length.
|
||||
|
||||
|
@ -57,6 +57,18 @@ MVC supports any attribute that derives from `ValidationAttribute` for validatio
|
|||
|
||||
There may be instances where you need more features than built-in attributes provide. For those times, you can create custom validation attributes by deriving from `ValidationAttribute` or changing your model to implement `IValidatableObject`.
|
||||
|
||||
## Notes on the use of the Required attribute
|
||||
|
||||
Non-nullable [value types](/dotnet/csharp/language-reference/keywords/value-types) (such as `decimal`, `int`, `float`, and `DateTime`) are inherently required and don't need the `Required` attribute. The app performs no validation checks server-side for non-nullable types decorated with `Required`.
|
||||
|
||||
MVC model binding rejects a property submission in the form data containing an empty string for a non-nullable value type. Model binding usually ignores missing data for properties.
|
||||
|
||||
`Required` can be used to change the client-side validation error message for a non-nullable value type property.
|
||||
|
||||
The [BindRequired attribute](/aspnet/core/api/microsoft.aspnetcore.mvc.modelbinding.bindrequiredattribute) (also see [Customize model binding behavior with attributes](xref:mvc/models/model-binding#customize-model-binding-behavior-with-attributes)) is useful to ensure form data is complete. When applied to a property, the model binding system requires a value for that property. When applied to a type, the model binding system requires values for all of the properties of that type.
|
||||
|
||||
When you create a [nullable type](/dotnet/csharp/programming-guide/nullable-types/) (for example, `decimal?`), the `Required` attribute works just like it does for ordinary nullable types.
|
||||
|
||||
## Model State
|
||||
|
||||
Model state represents validation errors in submitted HTML form values.
|
||||
|
|
|
@ -32,7 +32,7 @@ Update the `Movie` class to take advantage of the `Required`, `StringLength`, `R
|
|||
|
||||
[!code-csharp[Main](../../tutorials/first-mvc-app/start-mvc//sample/MvcMovie/Models/MovieDateRatingDA.cs?name=snippet1)]
|
||||
|
||||
Validation attributes specify behavior that is enforced on model properties. The `Required` and `MinimumLength` attributes indicates that a property must have a value; but nothing prevents a user from entering white space to satisfy the validation constraint. The `RegularExpression` attribute is used to limit what characters can be input. In the preceding code, `Genre` and `Rating` must use only letters (white space, numbers and special characters are not allowed). The `Range` attribute constrains a value to within a specified range. The `StringLength` attribute sets the maximum length of a string, and optionally the minimum length. Non-nullable [value types](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/value-types) (such as `decimal`, `int`, `float`, `DateTime`) are inherently required and don't need the `Required` attribute.
|
||||
Validation attributes specify behavior that is enforced on model properties. The `Required` and `MinimumLength` attributes indicate that a property must have a value, but nothing prevents a user from entering whitespace to satisfy the validation constraint for a nullable type. The `RegularExpression` attribute is used to limit what characters can be input. In the preceding code, `Genre` and `Rating` must use only letters (white space, numbers and special characters aren't allowed). The `Range` attribute constrains a value to a specified range. The `StringLength` attribute sets the maximum length of a string, and optionally the minimum length. Non-nullable [value types](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/value-types) (such as `decimal`, `int`, `float`, and `DateTime`) are inherently required and don't need the `Required` attribute.
|
||||
|
||||
Having validation rules automatically enforced by ASP.NET Core helps make an app more robust. Automatic validation on models helps protect the app because you don't have to remember to apply them when new code is added.
|
||||
|
||||
|
|
Loading…
Reference in New Issue