Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,820 --> 00:00:07,300
In the last section, we managed to use our form key right here to get a reference to the form St. instance
2
00:00:07,300 --> 00:00:08,380
that belongs to this form.
3
00:00:09,260 --> 00:00:14,750
Once we had that reference to the form states, we then called the reset method on the form states and
4
00:00:14,750 --> 00:00:18,740
we very clearly saw that that reset every one of the inputs within our form.
5
00:00:19,870 --> 00:00:24,790
So if we reference back to our diagram that we were looking at a little bit ago over here, we've clearly
6
00:00:24,790 --> 00:00:26,950
accomplished part number one of this.
7
00:00:26,950 --> 00:00:32,640
We have direct access to the form St. instance that represents our form on the screen.
8
00:00:33,430 --> 00:00:38,530
So now we can move on to part two of this, which is to tell it to validate all the text form fields
9
00:00:38,650 --> 00:00:41,450
that are nested underneath it to do so.
10
00:00:41,500 --> 00:00:45,280
Let's first take a look at some documentation and get a better sense of how we're going to use the form
11
00:00:45,280 --> 00:00:48,760
states to tell it to validate all the text form fields that are inside.
12
00:00:50,030 --> 00:00:57,500
OK, so I've got my form state documentation up, I'm going to go down to my method section, so in
13
00:00:57,500 --> 00:00:59,780
the last section we called Reset, there it is right there.
14
00:00:59,780 --> 00:01:01,130
So we're looking at the same section.
15
00:01:01,640 --> 00:01:05,650
And right underneath that, you'll notice that there's a method, another method called validate.
16
00:01:06,320 --> 00:01:12,500
So if we call validate, that's going to apparently validate every form field that is nested within
17
00:01:12,500 --> 00:01:13,040
this form.
18
00:01:14,090 --> 00:01:19,670
And then if they are all valid, if all those foreign fields are valid, then the overall validate call
19
00:01:19,700 --> 00:01:21,260
that we're going to make is going to return true.
20
00:01:21,720 --> 00:01:26,900
Otherwise, it will return false to indicate that one of our inputs has some invalid input inside of
21
00:01:26,900 --> 00:01:27,100
it.
22
00:01:28,150 --> 00:01:32,800
Now, when you look at this method right here and the description for Validate, this is a function
23
00:01:33,220 --> 00:01:40,330
that is tied to our form state class, you'll notice that the documentation doesn't say anything at
24
00:01:40,330 --> 00:01:43,080
all about how this validation is actually done.
25
00:01:43,780 --> 00:01:45,940
In other words, how are we going to make sure?
26
00:01:46,980 --> 00:01:51,960
That the text form field that represents our password actually is a password, like we need to make
27
00:01:51,960 --> 00:01:57,620
sure that whatever gets entered into this field right here specifically looks like a password.
28
00:01:57,630 --> 00:01:59,880
And so maybe it has to have a certain number of characters.
29
00:02:00,480 --> 00:02:06,120
And likewise, we need to make sure that when this validation step occurs, this text form a field that
30
00:02:06,120 --> 00:02:08,810
represents the email has to look like an email.
31
00:02:08,820 --> 00:02:13,160
So it has to have like, you know, an at character, at symbol inside of it or something like that.
32
00:02:13,890 --> 00:02:17,480
So the documentation here is kind of lacking in that regard.
33
00:02:17,490 --> 00:02:21,710
The documentation is not telling you how you set a validation up.
34
00:02:22,140 --> 00:02:27,480
And in fact, in my opinion, all the documentation, if you're not super clear on how you set up this
35
00:02:27,480 --> 00:02:28,400
validation stuff.
36
00:02:28,890 --> 00:02:33,000
So rather than trying to reference any documentation, I'm going to show you a diagram that's going
37
00:02:33,000 --> 00:02:35,940
to make this entire validation flow a little bit more obvious.
38
00:02:37,650 --> 00:02:38,990
OK, here we go.
39
00:02:39,780 --> 00:02:44,760
All right, so here's what happens right now, we're inside of our login screen or more precisely the
40
00:02:44,760 --> 00:02:51,480
login screen states, we've created our global key, which represents or kind of references the form
41
00:02:51,480 --> 00:02:52,220
state objects.
42
00:02:52,800 --> 00:02:57,090
And as we just saw, the form state has a method tied to it called validate.
43
00:02:57,270 --> 00:02:59,180
So that's the documentation we're just looking at.
44
00:02:59,190 --> 00:03:01,950
That's the validate function right here.
45
00:03:03,060 --> 00:03:09,840
So when we call validate, here's what happens, they validate function is going to look at all of the
46
00:03:09,840 --> 00:03:17,910
children of the form widget and it's going to find every single instance underneath the form of a text
47
00:03:17,910 --> 00:03:18,870
form fields.
48
00:03:20,310 --> 00:03:26,610
And so they validate function is going to automatically find this text form field and this text form
49
00:03:26,610 --> 00:03:27,000
field.
50
00:03:28,070 --> 00:03:35,420
Then on both of those text form field instances that validate function right here is going to call a
51
00:03:35,420 --> 00:03:42,470
function that you and I can optionally pass into the constructor of the text form field called validator.
52
00:03:43,900 --> 00:03:49,360
The validator function right here is what is actually responsible for looking at the value that has
53
00:03:49,360 --> 00:03:53,890
been entered into the text form field and saying whether or not it is valid.
54
00:03:55,090 --> 00:04:00,070
OK, so this validator right here might seem a little bit confusing, so let's go make one quick change
55
00:04:00,070 --> 00:04:03,640
inside of our code editor and you'll see where this validator thing comes into play.
56
00:04:04,750 --> 00:04:09,310
So I'm going to go back to my code, Ed. I'm going to find my email fields, here is the text form fields
57
00:04:09,460 --> 00:04:11,950
and as an additional property to the constructor.
58
00:04:13,000 --> 00:04:15,850
I'm going to pass in a function called validator.
59
00:04:17,589 --> 00:04:22,310
Or a property called validator to know a function to the validator property.
60
00:04:22,330 --> 00:04:22,780
There we go.
61
00:04:23,560 --> 00:04:30,250
So now this function right here is going to be automatically called any time we call the validate function
62
00:04:30,250 --> 00:04:31,240
on form state.
63
00:04:32,020 --> 00:04:37,150
So now inside that validator function, we're going to have the opportunity to inspect the value that
64
00:04:37,150 --> 00:04:39,000
was entered into our text form field.
65
00:04:39,640 --> 00:04:43,980
And if that value is valid and we like it, we're going to return nothing.
66
00:04:44,350 --> 00:04:49,120
Otherwise we will return a string that represents the error message that we want to show to the user.
67
00:04:49,690 --> 00:04:56,200
So essentially on this validator function, we're going to return null if valid.
68
00:04:57,070 --> 00:05:01,780
Otherwise string with the error message if invalid.
69
00:05:02,900 --> 00:05:07,850
OK, so hopefully now that makes a little bit more sense, so when we call validate on the form St.
70
00:05:07,850 --> 00:05:11,750
Instance, Validate is going to find all the nested text form fields.
71
00:05:12,050 --> 00:05:14,510
It's going to call the validator function on each one.
72
00:05:14,780 --> 00:05:16,640
It's going to collect all those things together.
73
00:05:16,670 --> 00:05:20,540
All the responses are all the return values from these different validator functions.
74
00:05:20,690 --> 00:05:25,220
And it's going to say, hey, are any of you not null like did any of you have an issue?
75
00:05:25,640 --> 00:05:30,740
And if there is an issue, then the validate function back up here belongs to the form St. instance
76
00:05:30,740 --> 00:05:33,410
is going to say, oh, sorry, no, that form is not valid.
77
00:05:33,410 --> 00:05:36,450
Like one of these fields is wrong and the user is going to have to go fix it.
78
00:05:37,550 --> 00:05:37,940
All right.
79
00:05:38,030 --> 00:05:42,470
So that's the behind the scenes look at what is going on with this validate stuff.
80
00:05:42,800 --> 00:05:44,090
So let's take a quick pause here.
81
00:05:45,080 --> 00:05:50,000
We're going to come back the next section and we're going to write out our validator function for both
82
00:05:50,000 --> 00:05:52,690
our email fields and our password fields.
83
00:05:52,790 --> 00:05:54,800
So quick break and I'll see you in just a minute.
8686
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.