Using and Creating Validators3:38 with Kenneth Love
Validators, whether custom or Django-provided, give us a great way to make sure the content of a field matches a condition we've set.
Did you notice my flip-flop in the video? Writing a custom validator is a great way to have reusable validation, so if I need to do something specific on a lot of fields, I definitely use a custom validator. In the example in this video, though, it just makes more sense to use Django's provided
MaxLengthValidator. Sorry if that was a bit confusing!
Django provides us with several validators already built in, so that we don't have to 0:00 handle things like checking for a maximum or minimum value, max or min length, or 0:03 a valid email or IP address, but our honey pot needs to be validated to be blank. 0:08 We're already doing this with our clean honey pot method, 0:14 but that's not easily reusable, so we should come up with something else. 0:16 Let's do this two ways. 0:20 The max length validator, and with a custom validator. 0:22 So we have this easy way of cleaning our honeypot, and 0:26 this is really good, but it's not something that is abstracted, 0:29 it's not something that we can apply to a bunch of different forms. 0:33 We could turn it into that, but 0:37 that's probably more trouble than it's really worth. 0:39 And we don't wanna have to write this method every single time 0:41 we use a honeypot. 0:44 We'd probably end up just copying and pasting it over, and over, and over again. 0:44 That's not the best idea, so what can we do instead? 0:49 Well, what we can do is we can bring in a core Django 0:53 feature called validators that validate data, so let's do that. 0:56 Let's get rid of our clean honeypot. 1:02 And let's come up here, and we want to do from django.core import validators. 1:05 So that just gives us all of our validators. 1:13 And here on our honeypot, we want to specify what the validators are. 1:16 So we can do validators=, and 1:21 we can put in validators.MaxLengthValidator. 1:25 And the max length is 0. 1:32 Length of zero, if there's any length at all, then this fails. 1:37 All right so let's come back over here and we can actually we've already got this set 1:42 up with the bad value, so let's just hit submit. 1:47 We should see another error message come out right up here. 1:49 Yep, so here we go hidden field honey pot ensure this value has at most 1:53 zero characters, it has seven. 1:57 So, great. 2:00 That's actually pretty simple. 2:01 And this actually works really well. 2:03 I like this. 2:05 This is probably how I would handle this situation. 2:06 Just give it this MaxLengthValidator of 0. 2:09 But sometimes you need something that's a little more complicated of a validation. 2:11 So let's see how to write a custom validator. 2:16 So we're just gonna come up here and we're gonna make a new function. 2:20 And I'm just gonna say must be empty. 2:24 And validators always get a value. 2:29 They get the value that's in the field that they're validating. 2:30 So we'll just call that value and we'll say if value. 2:34 If there's anything there at all. 2:37 If there's a value. 2:39 Then we want to raise forms.validationError. 2:41 And we're gonna say is not empty, 2:50 because really you're reflecting what the state of the field is. 2:52 So the state of the field is that the field isn't empty, and 2:57 that field should be empty. 3:00 So then in here, inside of the MaxLengthValidator, 3:01 we'll just put must_be_empty. 3:04 And then we'll come over here. 3:09 Everything is still filled in. 3:11 And if we submit this, we should see something new there. 3:12 And there we go, we have honeypot is not empty. 3:16 So three different ways to handle cleaning out that one field. 3:19 They all have their own strengths and weaknesses. 3:23 I'll leave it to you to decide which one of those solutions is better. 3:26 They both have pros and cons, but 3:29 I tend to like the CustomValidator a bit better because it's a bit more explicit. 3:30 Anyone applying that to their form fields will know exactly what it's for. 3:35
You need to sign up for Treehouse in order to download course files.Sign up