1 00:00:00,690 --> 00:00:01,660 Hello and welcome. 2 00:00:01,680 --> 00:00:10,620 In this picture we are going to add some validation to our form and we are going to add that in to our 3 00:00:10,720 --> 00:00:12,330 post model. 4 00:00:12,600 --> 00:00:19,060 So let's expand our Dairy Queen here and go to apps on lalley Apps. 5 00:00:19,100 --> 00:00:20,800 Those models are a cream. 6 00:00:20,840 --> 00:00:23,920 Inside there is a Post dot R B. 7 00:00:24,000 --> 00:00:27,330 So this is what the model looks like under my meds. 8 00:00:27,450 --> 00:00:29,530 Fairly basic. 9 00:00:29,700 --> 00:00:31,320 There isn't much to defile. 10 00:00:31,350 --> 00:00:34,540 Let's just explain a few things here. 11 00:00:34,830 --> 00:00:36,590 Post class. 12 00:00:36,600 --> 00:00:46,650 You can see here in inherits it's from the Application Record the Application Record inherits from the 13 00:00:46,800 --> 00:00:49,670 active record base. 14 00:00:50,660 --> 00:00:55,380 So this is the active record base and that's the application recaught. 15 00:00:55,400 --> 00:00:59,470 So this is a post model and this is application. 16 00:01:00,330 --> 00:01:06,320 Record and the Application Record in here is from this here. 17 00:01:06,660 --> 00:01:10,340 While the post clasp inherits from that. 18 00:01:12,000 --> 00:01:13,160 Let's take a quick look. 19 00:01:13,170 --> 00:01:19,040 The application recaught saw this active record base here. 20 00:01:19,440 --> 00:01:28,070 And what that does it actually supplies a great deal of functionality to your real models. 21 00:01:28,080 --> 00:01:30,150 It does this for free. 22 00:01:30,300 --> 00:01:35,010 It includes things like allows you to do kuroda operation so you can do create. 23 00:01:35,010 --> 00:01:37,070 You can read an obsolete. 24 00:01:37,200 --> 00:01:39,770 You can delete or destroy. 25 00:01:39,850 --> 00:01:47,380 He allows you to perform all those operations you also assist with forn validation as well. 26 00:01:47,380 --> 00:01:50,790 So let's go back into the post model here. 27 00:01:51,150 --> 00:01:58,710 Rails includes methods that helps you validate the data that you send to your models. 28 00:01:58,710 --> 00:02:02,260 We just don't want to send any type of data to the model. 29 00:02:02,430 --> 00:02:07,020 We want to validate what we are able to send to the models. 30 00:02:07,180 --> 00:02:19,620 So inside here we are going to apply or implement some validation here too so that it enforces what 31 00:02:19,770 --> 00:02:22,710 you are not allowed to post. 32 00:02:22,860 --> 00:02:27,510 So to implement the validation or you need to do is typing validate. 33 00:02:27,510 --> 00:02:35,160 So we're just trying to specify what the form should validate it also validates and then do as face 34 00:02:35,910 --> 00:02:38,330 call on we want that title. 35 00:02:39,800 --> 00:02:40,650 Okay. 36 00:02:40,760 --> 00:02:49,920 Comma and then presses means we want the process off a title and we set that value to be true. 37 00:02:50,350 --> 00:02:53,440 So do a space and do True. 38 00:02:54,050 --> 00:03:02,940 Next thing we want to do we want to specify the length of that okay so we're typing Len. 39 00:03:06,510 --> 00:03:09,010 K and we do a call on. 40 00:03:11,190 --> 00:03:19,590 I'm going to specify the minimum length of character that is going to be allowed for the title. 41 00:03:19,860 --> 00:03:24,820 So you can specify any and so you type p.m. on. 42 00:03:27,420 --> 00:03:35,280 Then a call on space and they you determine what the minimum character should be in my is I'm just going 43 00:03:35,280 --> 00:03:37,540 to see for I'm happy for. 44 00:03:37,560 --> 00:03:43,440 But you put whatever value you want to what that means when you enter a title for the post. 45 00:03:43,560 --> 00:03:47,100 It has to be a minimum of four character. 46 00:03:47,100 --> 00:03:49,870 That's basically what that EDITION. 47 00:03:50,260 --> 00:03:58,940 I'm not going to go file an UNDO save all you can validate a variety of things at the moment we've Wallace 48 00:03:58,950 --> 00:04:08,160 set the validation to the title so that he does set a minimum of four character long saw you can also 49 00:04:08,190 --> 00:04:16,500 validate things like the presents or the uniqueness of column the format of the column the existence 50 00:04:16,560 --> 00:04:19,150 of arsaell see it at objects ansorge. 51 00:04:19,180 --> 00:04:23,970 So there's lots of things that you can specify where the validation for. 52 00:04:23,970 --> 00:04:31,330 Now we're just going to keep it simple and just validate the title for now with the validation line 53 00:04:31,370 --> 00:04:40,740 place that we have to go back to our post controller and we have to call the Post dot sleeve so that 54 00:04:40,740 --> 00:04:45,500 when the when you press the same button on the form it all validates the. 55 00:04:45,550 --> 00:04:52,330 Cool back to our Controller we can control click Post. 56 00:04:52,710 --> 00:05:02,310 We want to go in to this say actions fellas quote With this save action here. 57 00:05:02,430 --> 00:05:06,580 Here we are going to add in an IF statement there. 58 00:05:06,600 --> 00:05:10,620 So implement your statement first and then I will explain why. 59 00:05:10,650 --> 00:05:12,460 Same thing if. 60 00:05:15,010 --> 00:05:24,770 The post was posted save what it is say it will return in force and if you redirect it. 61 00:05:26,210 --> 00:05:29,690 You can redirect it to the post. 62 00:05:29,810 --> 00:05:33,150 So what this is saying was just submit the form. 63 00:05:33,290 --> 00:05:42,400 Once the validation is so key if the validation is OK it will redirect to the show view. 64 00:05:42,980 --> 00:05:49,280 But if every time you implement an if statement is always good to add an else for the other condition. 65 00:05:49,340 --> 00:05:51,690 So this is if the validation is OK. 66 00:05:51,710 --> 00:05:53,050 This is what it will do. 67 00:05:53,240 --> 00:05:58,920 However the validation is not to say we want to implement something else. 68 00:05:58,970 --> 00:06:10,680 If the validation is not talk we want to render a new king and then we press end to end that. 69 00:06:12,020 --> 00:06:17,060 So what I want to do is wrap this eve steadman Rapid round it and see 70 00:06:19,790 --> 00:06:21,890 wrap that there and wrap that. 71 00:06:21,890 --> 00:06:27,630 So what should happen when the few press the see Button. 72 00:06:27,810 --> 00:06:31,330 It will check if everything is validated okay. 73 00:06:31,340 --> 00:06:35,130 It will redirect to the post for you. 74 00:06:35,430 --> 00:06:38,610 OK if nothing happens that is a sure view. 75 00:06:38,650 --> 00:06:46,100 And if that's not the case everything here doesn't go OK it will fall onto this L statement and render 76 00:06:46,730 --> 00:06:51,950 the form again so render the form you give you the new form again to try again. 77 00:06:51,950 --> 00:06:59,110 So let's save that and have a look at the form hopefully out for mr who run in. 78 00:06:59,180 --> 00:07:05,850 K. So now let's try and say that if we saved that we should get some validation warning if you notice 79 00:07:05,850 --> 00:07:06,020 this. 80 00:07:06,040 --> 00:07:06,520 No. 81 00:07:06,700 --> 00:07:08,470 No error messages here. 82 00:07:08,520 --> 00:07:10,980 He just returns back to the form. 83 00:07:10,980 --> 00:07:14,440 So we need to add SOM or k.. 84 00:07:14,460 --> 00:07:15,800 We've course on error here. 85 00:07:15,810 --> 00:07:19,540 Let's see was same is same with the more. 86 00:07:19,640 --> 00:07:20,570 Okay. 87 00:07:20,690 --> 00:07:26,250 Got an error here so I need to go back to the model and check that out. 88 00:07:27,250 --> 00:07:32,670 So I think the the error here I should have had a comma there. 89 00:07:33,010 --> 00:07:36,060 So let's put a comma there and safe. 90 00:07:37,460 --> 00:07:39,990 Because it doesn't await any US wives complaining. 91 00:07:40,010 --> 00:07:41,910 Let's go back and refresh hatch. 92 00:07:41,930 --> 00:07:43,650 Hopefully she'd be all right now. 93 00:07:44,550 --> 00:07:44,930 Okay. 94 00:07:44,950 --> 00:07:45,340 Yeah. 95 00:07:45,510 --> 00:07:47,110 So now let's try and see. 96 00:07:47,160 --> 00:07:50,380 This portrait kit at the moment. 97 00:07:50,430 --> 00:07:56,220 We try and save it it returns us to it gives us the form the new form is gabry renders the form. 98 00:07:56,400 --> 00:08:01,750 So what I want to do in the next lecturer is try to implement som error or handling. 99 00:08:01,830 --> 00:08:03,970 So we know what's actually happening. 100 00:08:03,970 --> 00:08:09,870 So if you SOB if you save the pause he'll tell you there's any error messages at the moment is just 101 00:08:09,870 --> 00:08:14,620 re rendering the form which is what I've specified here. 102 00:08:14,640 --> 00:08:16,480 So that's what is in here. 103 00:08:16,500 --> 00:08:17,440 If posed. 104 00:08:17,460 --> 00:08:23,940 If you press that he redirect you to depose show or else he renders a new form read. 105 00:08:23,970 --> 00:08:24,740 Does the former. 106 00:08:24,750 --> 00:08:26,140 Which is what he's doing. 107 00:08:26,400 --> 00:08:30,220 So we need to implement error or handling in the next lecture. 108 00:08:30,540 --> 00:08:31,920 So thanks for watching. 109 00:08:31,920 --> 00:08:32,810 Bye for now.