1 00:00:00,290 --> 00:00:04,870 Having the ability to write code to validate our model is a powerful tool, but 2 00:00:04,870 --> 00:00:07,020 it can quickly get tedious. 3 00:00:07,020 --> 00:00:11,614 As an alternative NVC allows us to express simple validation rules 4 00:00:11,614 --> 00:00:14,328 using data annotations on our models. 5 00:00:14,328 --> 00:00:16,327 Let's open our entry data model. 6 00:00:19,316 --> 00:00:23,530 I'll collapse to definitions by pressing Ctrl+M, Ctrl+O. 7 00:00:23,530 --> 00:00:27,840 If we look at our interim model properties the NPC model binder will automatically 8 00:00:27,840 --> 00:00:33,080 validate each of the non-nullable value type properties as required fields. 9 00:00:33,080 --> 00:00:36,852 So Date, ActivityId, 10 00:00:36,852 --> 00:00:42,402 Duration, Intensity and 11 00:00:42,402 --> 00:00:47,607 Exclude are all automatically required fields. 12 00:00:50,776 --> 00:00:54,687 The Notes property is of type string which allows nulls, so 13 00:00:54,687 --> 00:00:59,950 that property would not automatically be validated as a required field. 14 00:00:59,950 --> 00:01:03,140 We can easily add a required validation for that field 15 00:01:03,140 --> 00:01:07,940 by adding a required data annotation attribute to the notes property. 16 00:01:07,940 --> 00:01:11,090 But that's not all, what if we want to prevent users 17 00:01:11,090 --> 00:01:14,600 from providing notes longer than 200 characters? 18 00:01:14,600 --> 00:01:18,980 Easy, we just add a max link data annotation attribute 19 00:01:18,980 --> 00:01:21,190 right below the required attribute. 20 00:01:21,190 --> 00:01:25,590 It requires a parameter value indicating what the max link should be. 21 00:01:25,590 --> 00:01:29,630 We can also set the error message property with the message that we want to display 22 00:01:29,630 --> 00:01:32,250 to users when this validation fails. 23 00:01:32,250 --> 00:01:39,790 Let's use the notes field cannot be longer than 200 characters. 24 00:01:41,410 --> 00:01:47,230 These are just two of the data annotations that are available for us to use. 25 00:01:47,230 --> 00:01:54,580 Other data annotations include min length, range, email address, phone, and more. 26 00:01:54,580 --> 00:01:58,150 You can even create your own custom data annotations. 27 00:01:58,150 --> 00:02:02,330 For more information about the built in data annotations or 28 00:02:02,330 --> 00:02:06,560 how to create custom data annotations, see the teacher's notes. 29 00:02:06,560 --> 00:02:09,807 Let's test our duration and notes field validation rules. 30 00:02:09,807 --> 00:02:11,816 Press F5 to run the app 31 00:02:15,501 --> 00:02:21,657 Then select an activity value and save the form. 32 00:02:21,657 --> 00:02:24,760 That should trigger the duration greater than zero and 33 00:02:24,760 --> 00:02:26,789 notes required validation rules. 34 00:02:29,264 --> 00:02:33,971 In the Auto's window let's drill down into model state. 35 00:02:33,971 --> 00:02:39,789 Values, the third item, then errors. 36 00:02:43,219 --> 00:02:45,673 Here's the duration error message. 37 00:02:45,673 --> 00:02:48,529 The duration field value must be greater than zero. 38 00:02:48,529 --> 00:02:50,258 Then the sixth item. 39 00:02:52,995 --> 00:02:59,040 Errors, and here's the notes error message. 40 00:02:59,040 --> 00:03:01,190 The Notes field is required. 41 00:03:01,190 --> 00:03:04,170 Now let's test the notes max length validation. 42 00:03:04,170 --> 00:03:05,500 Press F5 to continue. 43 00:03:06,880 --> 00:03:10,660 Enter a value greater than zero for the duration field, and 44 00:03:10,660 --> 00:03:13,400 a long string of text for the notes field. 45 00:03:13,400 --> 00:03:15,315 I'll enter some random text here. 46 00:03:18,990 --> 00:03:21,470 And then copy and paste it a number of times. 47 00:03:25,672 --> 00:03:28,207 That feels more than 200 characters long. 48 00:03:28,207 --> 00:03:29,764 Okay, save the form. 49 00:03:36,080 --> 00:03:39,932 And here's the error message for the max length validation rule, 50 00:03:39,932 --> 00:03:41,458 go ahead and stop the app. 51 00:03:44,263 --> 00:03:48,930 Before we wrap up this video let's undo one of the changes that we made. 52 00:03:48,930 --> 00:03:52,040 I added the required validation rule to the notes property 53 00:03:52,040 --> 00:03:56,330 to demonstrate how easy it is to make a nullable property required. 54 00:03:56,330 --> 00:03:59,480 I didn't really intend to make the notes field required. 55 00:03:59,480 --> 00:04:03,900 So let's remove the required data annotation from the notes property. 56 00:04:03,900 --> 00:04:06,900 That will make it easier to test our app going forward. 57 00:04:06,900 --> 00:04:09,390 If you're using GitHub, let's commit our changes. 58 00:04:11,390 --> 00:04:16,748 Enter a commit message of, added validation rules using 59 00:04:16,748 --> 00:04:21,780 data annotations, and click the Commit All button. 60 00:04:23,610 --> 00:04:26,010 Having validation rules is great, but 61 00:04:26,010 --> 00:04:29,900 unfortunately our users currently can't see them. 62 00:04:29,900 --> 00:04:34,779 In the next video, we'll update our Add Entry view to display validation messages.