1 00:00:00,660 --> 00:00:02,470 Now that we know how to detect errors and 2 00:00:02,470 --> 00:00:06,680 redirect back to a form when we do see an error we have three tasks left. 3 00:00:06,680 --> 00:00:10,390 First, we'll need a place to display those errors in our Thymeleaf templates. 4 00:00:10,390 --> 00:00:12,860 Second, we need to make error messages 5 00:00:12,860 --> 00:00:15,800 available to the templates from our controller. 6 00:00:15,800 --> 00:00:20,660 And last we'll also make available to our Thymeleaf templates any data that 7 00:00:20,660 --> 00:00:25,880 the user had previously entered, so that it doesn't have to be retyped, ready? 8 00:00:25,880 --> 00:00:26,380 Let's do it. 9 00:00:27,620 --> 00:00:31,510 First, let's tackle the issue of redisplaying the data a user has entered 10 00:00:31,510 --> 00:00:35,710 if validation has failed, so that it doesn't have to be retyped. 11 00:00:35,710 --> 00:00:38,600 This is where we can take advantage of that timely 12 00:00:38,600 --> 00:00:41,280 object binding we talked about earlier in the course. 13 00:00:41,280 --> 00:00:44,710 Let me open the category form to remind you of that, 14 00:00:44,710 --> 00:00:48,070 here in the form element is where we bound. 15 00:00:48,070 --> 00:00:53,130 That object called category and inside this form element, 16 00:00:53,130 --> 00:00:54,620 that is some of its child elements. 17 00:00:54,620 --> 00:00:57,660 Namely, the input and select element 18 00:00:57,660 --> 00:01:02,560 refer to some of the properties of that bound object using this asterisk here. 19 00:01:03,990 --> 00:01:08,390 Now as long as Thymeleaf has an object name category in the model map, 20 00:01:08,390 --> 00:01:12,870 it will use the categories properties in pre populating the form field values. 21 00:01:12,870 --> 00:01:16,270 In our case, the name and the color code. 22 00:01:16,270 --> 00:01:18,090 So back to the category controller. 23 00:01:19,300 --> 00:01:23,490 What we need to do is add this category that's passed into this controller method 24 00:01:23,490 --> 00:01:25,100 here to the model map. 25 00:01:25,100 --> 00:01:27,360 The problem is that we're redirecting, so 26 00:01:27,360 --> 00:01:31,190 that if we were to include a model parameter here and add it as an attribute, 27 00:01:31,190 --> 00:01:35,500 that attribute wouldn't survive past the processing of this current request. 28 00:01:35,500 --> 00:01:40,490 But we do need it to survive the redirect that is we need it to be available 29 00:01:40,490 --> 00:01:43,700 on the next request, the one that we're redirecting to. 30 00:01:43,700 --> 00:01:47,270 That is this categories/ add request. 31 00:01:47,270 --> 00:01:49,890 Thankfully, Spring offers a solution for this and 32 00:01:49,890 --> 00:01:52,180 that is the redirect attributes object. 33 00:01:52,180 --> 00:01:55,350 If we add a redirect attributes parameter to this method, 34 00:01:55,350 --> 00:02:00,190 any flash attributes added will survive exactly one redirect. 35 00:02:00,190 --> 00:02:03,910 So let me add a RedirectAttributes parameter here. 36 00:02:03,910 --> 00:02:08,260 RedirectAttributes and I will call it the same thing. 37 00:02:09,900 --> 00:02:14,810 And now, if we do detect errors, let's include the category object that 38 00:02:14,810 --> 00:02:17,360 was submitted so that it's available upon redirect. 39 00:02:17,360 --> 00:02:23,150 So here, I'm gonna add the category if invalid data was received. 40 00:02:25,420 --> 00:02:28,890 So the way we'll do that is the following. 41 00:02:28,890 --> 00:02:33,030 We will use the add flash attribute method that will make the attribute survive 42 00:02:33,030 --> 00:02:39,170 exactly one redirect and any subsequent requests won't include this exact data. 43 00:02:39,170 --> 00:02:43,990 Here's how it looks, we'll use that redirect attributes parameter value and 44 00:02:43,990 --> 00:02:47,790 we will addFlashAttribute just like this. 45 00:02:47,790 --> 00:02:52,790 We'll call it category and we'll add whatever category 46 00:02:52,790 --> 00:02:57,950 was passed into this method which would be a compilation of values that 47 00:02:57,950 --> 00:03:02,730 came from the POST Request that is from the form data that the user submitted. 48 00:03:03,820 --> 00:03:08,085 Before we take our changes for a spend, let's take a peek at one more item. 49 00:03:08,085 --> 00:03:10,495 Think about this if evaluation fails, 50 00:03:10,495 --> 00:03:13,075 we're gonna redirect back to this add form. 51 00:03:13,075 --> 00:03:18,025 Now let's go to the method that renders this view and 52 00:03:18,025 --> 00:03:22,875 keep in mind that we're adding a category object here to be available for 53 00:03:22,875 --> 00:03:25,435 exactly one redirect. 54 00:03:25,435 --> 00:03:30,145 Let's go up to the method that processes the form itself. 55 00:03:31,330 --> 00:03:32,950 Let me find that one. 56 00:03:32,950 --> 00:03:34,620 Here it is, formNewCategory. 57 00:03:35,930 --> 00:03:37,140 Looking closely, 58 00:03:37,140 --> 00:03:43,020 it appears that even if we redirected with the category item included in the model. 59 00:03:43,020 --> 00:03:48,030 We are replacing it here with a newly constructed category object which will 60 00:03:48,030 --> 00:03:53,280 blow away all of the data that was previously entered by the user. 61 00:03:53,280 --> 00:03:58,920 So let's make sure not to overwrite that if one does exist and 62 00:03:58,920 --> 00:04:01,260 the way we do that is the following. 63 00:04:01,260 --> 00:04:08,346 We say, if the (!model.containsAttribute("category")), 64 00:04:08,346 --> 00:04:13,540 then we'll add a new category to the model. 65 00:04:13,540 --> 00:04:17,230 And if it does contain a model attribute named category. 66 00:04:17,230 --> 00:04:21,160 Well, we won't add another one because that must mean a user had submitted 67 00:04:21,160 --> 00:04:22,310 invalid data. 68 00:04:22,310 --> 00:04:25,420 And we added a category attribute that included that category 69 00:04:25,420 --> 00:04:30,660 object with invalid data and that will be the one we want to use in the form. 70 00:04:30,660 --> 00:04:33,340 All right, with that in place, let's reboot this app. 71 00:04:33,340 --> 00:04:36,510 And see if we successfully redirect if there are errors and 72 00:04:36,510 --> 00:04:39,550 include the previously entered data in the form. 73 00:04:39,550 --> 00:04:45,820 So I will stop my previous instance of the app and i will rerun that. 74 00:04:48,700 --> 00:04:52,290 Looks like it compiled successfully and we get all the way down 75 00:04:52,290 --> 00:04:56,500 to the message that says the application started successfully. 76 00:04:56,500 --> 00:04:58,930 Cool, let me switch to Chrome now. 77 00:04:59,970 --> 00:05:04,925 Let me try to add a category here and I will specify a color, but 78 00:05:04,925 --> 00:05:08,370 I will completely omit the Category Name. 79 00:05:08,370 --> 00:05:11,070 So i will Add that and great! 80 00:05:11,070 --> 00:05:13,998 We're back at the form with the color that we previously chose, so 81 00:05:13,998 --> 00:05:16,120 we don't have to choose that again. 82 00:05:16,120 --> 00:05:19,670 Now let's check out that size validation by trying to add a category with 83 00:05:19,670 --> 00:05:22,521 both say two characters, say Hi. 84 00:05:22,521 --> 00:05:27,350 And I will add that and there again, we're back at the form seeing 85 00:05:27,350 --> 00:05:31,480 the data we already entered so we don't have to re-enter that. 86 00:05:31,480 --> 00:05:32,420 Now finally, 87 00:05:32,420 --> 00:05:37,599 let's make sure we can still successfully add a category of all data is valid. 88 00:05:37,599 --> 00:05:45,020 Let me finish this here with Hibernate and click Add, great! 89 00:05:45,020 --> 00:05:47,120 We're done with that part and next, 90 00:05:47,120 --> 00:05:51,678 we'll tackle the display of validation messages in our Thymeleaf templates.