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