From 41436686226e8d90325748722298c4506b127e0b Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 17 Feb 2023 23:00:20 +0100 Subject: [PATCH 1/7] =?UTF-8?q?changed=20date=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aspnetcore/mvc/models/validation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 5c5351ace607..1e4638187556 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -5,7 +5,7 @@ description: Learn about model validation in ASP.NET Core MVC and Razor Pages. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 02/02/2022 +ms.date: 02/17/2023 uid: mvc/models/validation --- # Model validation in ASP.NET Core MVC and Razor Pages From 624f801dc51249990f6368dfcb4cb54f302f0df5 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Fri, 17 Feb 2023 14:23:29 -1000 Subject: [PATCH 2/7] IResult model binding and testing /2 --- aspnetcore/mvc/models/validation.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 1e4638187556..c5a268861e2a 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -10,6 +10,10 @@ uid: mvc/models/validation --- # Model validation in ASP.NET Core MVC and Razor Pages + :::moniker range=">= aspnetcore-7.0" This article explains how to validate user input in an ASP.NET Core MVC or Razor Pages app. From 8deb5bb9a0a6a1afcb8780a5829c008c4dcb4fed Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:38:31 -1000 Subject: [PATCH 3/7] work --- aspnetcore/mvc/models/validation.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index c5a268861e2a..8a43ea7bac56 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -10,10 +10,6 @@ uid: mvc/models/validation --- # Model validation in ASP.NET Core MVC and Razor Pages - :::moniker range=">= aspnetcore-7.0" This article explains how to validate user input in an ASP.NET Core MVC or Razor Pages app. @@ -213,6 +209,24 @@ The preceding example works only with `Movie` types. Another option for class-le :::code language="csharp" source="~/mvc/models/validation/samples/6.x/ValidationSample/Models/ValidatableMovie.cs" id="snippet_Class" highlight="1,26-34"::: +## Custom validation + +The following code shows how to add a model error after examining the model: + +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_5""::: + +The following code implements the validation test in a controller: + +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_5""::: + +The following code verifies the phone number and email are unique: + +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_5""::: + +The following code implements the validation test in a controller: + +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_5""::: + ## Top-level node validation Top-level nodes include: From 072bcafea0e9c7a9fdb388af1b3e608dec32ad47 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:21:30 -1000 Subject: [PATCH 4/7] work --- aspnetcore/mvc/models/validation.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 8a43ea7bac56..6521c4ed9b7c 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -213,19 +213,19 @@ The preceding example works only with `Movie` types. Another option for class-le The following code shows how to add a model error after examining the model: -:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_5""::: +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_5"::: The following code implements the validation test in a controller: -:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_5""::: +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_1"::: The following code verifies the phone number and email are unique: -:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_5""::: +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs" id="snippet_1"::: The following code implements the validation test in a controller: -:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_5""::: +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_4"::: ## Top-level node validation From b1ecd89b6cc935cb081d8ad2a048d6a89e782aad Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:33:07 -1000 Subject: [PATCH 5/7] work --- aspnetcore/mvc/models/validation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 6521c4ed9b7c..9aadf4d7e691 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -217,7 +217,7 @@ The following code shows how to add a model error after examining the model: The following code implements the validation test in a controller: -:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_1"::: +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_5"::: The following code verifies the phone number and email are unique: From 3e492b06c542e0fac45717fdaddccfc6552f8f17 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:36:57 -1000 Subject: [PATCH 6/7] work --- aspnetcore/mvc/models/validation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 9aadf4d7e691..25253b09836a 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -227,6 +227,8 @@ The following code implements the validation test in a controller: :::code language="csharp" source="~/../AspNetCore.Docs.Samples/mvc/models/ModelStateError/Controllers/ContactsController.cs" id="snippet_4"::: +Checking for a unique phone number or email is typically also done with [remote validation](#remote-attribute). + ## Top-level node validation Top-level nodes include: From 296922be5fc5f1c43d95ee4a5863fdb24f1bbcc7 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:39:20 -1000 Subject: [PATCH 7/7] work --- aspnetcore/mvc/models/validation.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/validation.md b/aspnetcore/mvc/models/validation.md index 25253b09836a..4a1999c47eb0 100644 --- a/aspnetcore/mvc/models/validation.md +++ b/aspnetcore/mvc/models/validation.md @@ -158,7 +158,9 @@ To implement remote validation: 1. In the model class, annotate the property with a `[Remote]` attribute that points to the validation action method, as shown in the following example: :::code language="csharp" source="~/mvc/models/validation/samples/6.x/ValidationSample/Models/User.cs" id="snippet_Email"::: - + +[Server side validation](#custom-validation) also needs to be implemented for clients that have disabled JavaScript. + ### Additional fields The property of the `[Remote]` attribute lets you validate combinations of fields against data on the server. For example, if the `User` model had `FirstName` and `LastName` properties, you might want to verify that no existing users already have that pair of names. The following example shows how to use `AdditionalFields`: