Cleaning a Whole Form4:28 with Kenneth Love
Sometimes you need to clean several fields together or you just need a check on the *entire* form.
You'll find that a lot of beginners use
clean() way more often than they use validators or
clean_FIELDNAME(). Don't fall into this habit! When you're working with Django, and pretty much any other framework, you want to override and customize the smallest possible piece. It's less impact on your form to override
clean_email and check a length or value than to override
clean() and do the same. Always try to find what creates the least impact.
We've seen two different ways to validate a single field. 0:00 We can do it with clean field or 0:03 with a validator, either one that Jango provides, or one that we write ourselves. 0:06 All of these methods, though, only let us handle a single field at a time. 0:10 What if we need to check two or 0:14 more fields together to know if the one was valid? 0:16 This is where the form's clean method comes into play. 0:19 Let's go to workspaces. 0:22 All right, so our suggestion form is good. 0:24 One thing we probably wanna add, just to make this a more complete form 0:26 is we want somebody to verify their email address. 0:31 Let's go over here, let's get rid of this inspector. 0:33 We don't need that anymore. 0:36 So we want them to verify their e-mail address, and 0:38 let's do that by adding in another field. 0:40 So we'll say verify_email = forms.EmailField, and for 0:43 the help_text, let's put in, "Please verify your email address". 0:48 And now let's see what this does to our view, or form. 0:56 So we have our name like normal, we have email, and we have verify email, and 1:02 below that it says, Please verify your email address. 1:06 We can do that as a label instead of as the help text if we wanted. 1:09 We could be wanting to change this right here over to label, 1:12 and then Refresh, there we go, 1:17 and that's maybe a little more clean, and what you'd expect either way it works, 1:22 the help text is really handy when you're making forms for the admin. 1:29 And now though, we need to make sure that if put in my name here, 1:33 and I put in kenneth.example.com here, but I put in firstname.lastname@example.org here, 1:39 it should throw an error, and go, hey, you didn't give me the same email address. 1:49 So how do we do that? 1:54 We do that by specifying a clean method for the entire form. 1:56 So notice this clean method is just called clean. 2:02 There's no field name after it. 2:04 So this will clean the entire form. 2:07 So everybody go through every field and 2:09 make sure the field satisfies its own requirements. 2:10 Then we can go through the entire form as a whole and 2:14 make sure the form satisfies its requirements. 2:16 So, we need to get a couple of things in here. 2:20 So first of all let's get all of the data. 2:22 And we're gonna call super.clean. 2:27 That calls the clean method on our forms.form, or on our suggestion form. 2:29 So, that gets all of our cleaned data, it give us all of our data back. 2:36 So then we'll do email = cleaned_data. 2:40 And you can either do email or you could do, .get( 'email' 2:45 ) it acts like a dictionary, so, you can do either one of these. 2:51 And and then we'll do verify, cleaned_data.get( 'verify_email' ). 2:57 And so now, what we wanna do is we wanna make sure that the two of 3:04 those have the same data and are both filled in. 3:07 We already know they're both gonna be filled in, because it's gonna come back. 3:11 So we can just do, if email does not equal verify. 3:14 So if they don't have the same data in them, 3:19 then we want to raise forms.ValidationError. 3:22 And our error, we're gonna put in here, we'll just say, 3:26 "You need to enter the same email in both fields". 3:31 All right, so that's it. 3:37 Now we don't have to return anything from clean. 3:38 Unlike clean field, 3:41 like clean honeypot, you don't have to return anything back from clean. 3:42 It's cool with just running through and if there's no errors then it's happy. 3:46 So, this is bad. 3:49 Let's go ahead and suggest that and we'll say, Data security! 3:51 Oops, there we go. 3:57 That's the thing I want somebody to cover. 3:58 And if we hit that we get you need to enter the same email in both fields. 4:01 So great we got an error message about our two fields. 4:05 The fields are both, one of them is wrong. 4:08 We don't know which one. 4:11 So they both need to match. 4:12 Between cleaning fields, validators, and cleaning whole forms, 4:14 we're definitely able to make sure that our data is exactly what we want and need. 4:18 Okay, let's leave forms alone for a little bit and 4:23 go make some more interesting models for our next feature. 4:26
You need to sign up for Treehouse in order to download course files.Sign up