Implementing Server-Side Validation Using Data Annotations4:36 with James Churchill
Now let’s see how we can use data annotations to write less code when implementing server-side validations.
To follow along commiting your changes to this course, you'll need to fork the aspnet-fitness-frog repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-fitness-frog git checkout tags/v4.3 -b implementing-server-side-validation-using-data-annotations
For more information about C# Attributes, see this page on MSDN.
For additional information on how ASP.NET MVC validation works, including another look at using data annotations, see this ASP.NET website tutorial.
Here’s a project that extends the built-in data annotations.
Here’s an article that walks through the steps to create a custom data annotation attribute.
CTRL-M CTRL-O- Collapse to definitions
Having the ability to write code to validate our model is a powerful tool, but 0:00 it can quickly get tedious. 0:04 As an alternative NVC allows us to express simple validation rules 0:07 using data annotations on our models. 0:11 Let's open our entry data model. 0:14 I'll collapse to definitions by pressing Ctrl+M, Ctrl+O. 0:19 If we look at our interim model properties the NPC model binder will automatically 0:23 validate each of the non-nullable value type properties as required fields. 0:27 So Date, ActivityId, 0:33 Duration, Intensity and 0:36 Exclude are all automatically required fields. 0:42 The Notes property is of type string which allows nulls, so 0:50 that property would not automatically be validated as a required field. 0:54 We can easily add a required validation for that field 0:59 by adding a required data annotation attribute to the notes property. 1:03 But that's not all, what if we want to prevent users 1:07 from providing notes longer than 200 characters? 1:11 Easy, we just add a max link data annotation attribute 1:14 right below the required attribute. 1:18 It requires a parameter value indicating what the max link should be. 1:21 We can also set the error message property with the message that we want to display 1:25 to users when this validation fails. 1:29 Let's use the notes field cannot be longer than 200 characters. 1:32 These are just two of the data annotations that are available for us to use. 1:41 Other data annotations include min length, range, email address, phone, and more. 1:47 You can even create your own custom data annotations. 1:54 For more information about the built in data annotations or 1:58 how to create custom data annotations, see the teacher's notes. 2:02 Let's test our duration and notes field validation rules. 2:06 Press F5 to run the app 2:09 Then select an activity value and save the form. 2:15 That should trigger the duration greater than zero and 2:21 notes required validation rules. 2:24 In the Auto's window let's drill down into model state. 2:29 Values, the third item, then errors. 2:33 Here's the duration error message. 2:43 The duration field value must be greater than zero. 2:45 Then the sixth item. 2:48 Errors, and here's the notes error message. 2:52 The Notes field is required. 2:59 Now let's test the notes max length validation. 3:01 Press F5 to continue. 3:04 Enter a value greater than zero for the duration field, and 3:06 a long string of text for the notes field. 3:10 I'll enter some random text here. 3:13 And then copy and paste it a number of times. 3:18 That feels more than 200 characters long. 3:25 Okay, save the form. 3:28 And here's the error message for the max length validation rule, 3:36 go ahead and stop the app. 3:39 Before we wrap up this video let's undo one of the changes that we made. 3:44 I added the required validation rule to the notes property 3:48 to demonstrate how easy it is to make a nullable property required. 3:52 I didn't really intend to make the notes field required. 3:56 So let's remove the required data annotation from the notes property. 3:59 That will make it easier to test our app going forward. 4:03 If you're using GitHub, let's commit our changes. 4:06 Enter a commit message of, added validation rules using 4:11 data annotations, and click the Commit All button. 4:16 Having validation rules is great, but 4:23 unfortunately our users currently can't see them. 4:26 In the next video, we'll update our Add Entry view to display validation messages. 4:29
You need to sign up for Treehouse in order to download course files.Sign up