Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,390 --> 00:00:06,900
We've almost finished validating the form the next two fields are the country and terms of service fields.
2
00:00:07,170 --> 00:00:09,900
One is a dropdown and the other is a checkbox.
3
00:00:10,140 --> 00:00:15,690
Fortunately, even though they're are different elements, the process of validating them is still the
4
00:00:15,690 --> 00:00:16,170
same.
5
00:00:16,350 --> 00:00:23,010
In this lecture, we'll take the time to explore more rules and scenarios you may run into when validating
6
00:00:23,010 --> 00:00:23,560
a form.
7
00:00:24,030 --> 00:00:30,630
Let's start with the country fields in the template search for the select element in the form it'll
8
00:00:30,630 --> 00:00:30,750
be.
9
00:00:30,750 --> 00:00:37,230
A dropdown for a list of countries will change the select element to the the field component.
10
00:00:39,820 --> 00:00:46,320
The element the field component will generate will be an input element, we want to override this behavior.
11
00:00:46,600 --> 00:00:53,350
We can change the name of the tag by adding the as an attribute to the component will set this to select.
12
00:00:55,940 --> 00:01:02,030
This value will generate the desired tank inside the contents of the component we're passing in, the
13
00:01:02,030 --> 00:01:08,190
options for the dropdown validate will take care of passing these on to the select element.
14
00:01:08,510 --> 00:01:11,460
We don't need to modify the contents of the component.
15
00:01:12,080 --> 00:01:18,320
The next thing we'll do is assign a name to the component will set the name attribute to country.
16
00:01:20,990 --> 00:01:27,190
We can work on the rules next, the advantage to using a dropdown is limiting the possible choices.
17
00:01:27,410 --> 00:01:29,540
We don't have to worry about formatting.
18
00:01:29,930 --> 00:01:33,740
That makes things easier because we don't have to add as many rules.
19
00:01:33,980 --> 00:01:37,560
We don't need to worry about a minimum or maximum character length.
20
00:01:37,820 --> 00:01:39,710
This makes things straightforward.
21
00:01:40,070 --> 00:01:46,430
We'll want to add the required rule because we want the user to make a choice in the schema.
22
00:01:46,430 --> 00:01:49,670
Data object will update the rules for the country.
23
00:01:49,670 --> 00:01:52,190
Fields will set it to required.
24
00:01:54,750 --> 00:02:01,410
Lastly, we'll deal with the error, we'll use the same HTML we used for the other error messages,
25
00:02:01,830 --> 00:02:08,400
copy the error message component from any of the other inputs, paste it below the dropdown of countries.
26
00:02:10,889 --> 00:02:13,800
Will update the name attribute to country.
27
00:02:16,440 --> 00:02:22,950
The country field is pretty much ready, it's not very interesting, let's add a special rule for excluding
28
00:02:22,950 --> 00:02:24,840
a specific value for fun.
29
00:02:25,290 --> 00:02:30,810
Let's say sometime in the future you may want to prevent an option from being selected.
30
00:02:31,350 --> 00:02:35,870
For example, let's say a problem occurs in a country on this list.
31
00:02:36,030 --> 00:02:41,670
A new law or regulation has been passed that prevents us from providing service to that country.
32
00:02:41,880 --> 00:02:46,650
In the meantime, you need to prevent users from this country from registering.
33
00:02:47,250 --> 00:02:52,320
We can add a rule that will prevent users from registering if they select a certain option.
34
00:02:52,650 --> 00:02:56,340
Let's add a new option to the dropdown called Antarctica.
35
00:03:02,550 --> 00:03:09,180
I'm well aware that Antarctica is a continent and not a country where adding in as an example of a location
36
00:03:09,180 --> 00:03:15,300
we can't provide service to if the user selects Antarctica, will want to let the user know that they
37
00:03:15,300 --> 00:03:16,820
can't select that option.
38
00:03:17,250 --> 00:03:21,480
First, we'll need to register the rule, open the validation file.
39
00:03:24,110 --> 00:03:27,620
The rule will want to import is called not one of.
40
00:03:29,950 --> 00:03:35,290
It's a weird name I prefer to rename it to Excluded for a better description of the rule.
41
00:03:38,020 --> 00:03:42,340
Then we'll register the rule by calling the define rule function.
42
00:03:44,940 --> 00:03:51,420
The excluded rule will check if the value is in the list of invalid values, if it is, it'll throw
43
00:03:51,420 --> 00:03:55,290
an error switch back to the authentication component file.
44
00:03:57,850 --> 00:04:04,380
We're going to add the excluded rule, the option is the list of values, the input must not match.
45
00:04:04,570 --> 00:04:06,280
We'll add Antarctica.
46
00:04:08,860 --> 00:04:15,550
This rule will assume every value as a string, if you want to add more invalid values, you can separate
47
00:04:15,550 --> 00:04:16,540
them with the comma.
48
00:04:17,170 --> 00:04:18,420
We're almost finished.
49
00:04:18,610 --> 00:04:23,980
The very last field is a checkbox validating a checkboxes, relatively simple.
50
00:04:24,190 --> 00:04:30,580
Unlike the other input fields, we don't have to worry about validating the value because it's predetermines.
51
00:04:31,120 --> 00:04:34,100
The only thing we need to check for is if it's ticked.
52
00:04:34,570 --> 00:04:38,670
We want users to agree to the terms of service before registering.
53
00:04:39,310 --> 00:04:45,850
Let's go over to the checkbox in our form will convert the input element to a field component.
54
00:04:48,520 --> 00:04:54,580
Another attribute we have to set it, the name attribute will set it to toss.
55
00:04:56,990 --> 00:05:03,620
One last thing we need to add a value to the check box, if it's ticked, will set the value attribute
56
00:05:03,620 --> 00:05:04,220
to one.
57
00:05:06,900 --> 00:05:13,760
By default, the field component will use undefined as the value we want to make sure there's a value,
58
00:05:14,010 --> 00:05:17,410
otherwise any rules we add will always be broken.
59
00:05:18,060 --> 00:05:20,970
Let's work on adding some rules in the schema.
60
00:05:20,970 --> 00:05:25,320
Object will add the required rule to the Topsfield.
61
00:05:27,800 --> 00:05:33,680
The last thing we'll work on is the error, it'll be the same procedure, we'll go back to the template.
62
00:05:33,920 --> 00:05:38,600
We'll need to copy and paste the same error message we had for the other inputs.
63
00:05:38,870 --> 00:05:40,820
I'll grab it from the other inputs.
64
00:05:43,270 --> 00:05:46,840
Lastly, will update the expression to toss.
65
00:05:49,460 --> 00:05:50,870
That will finish things up.
66
00:05:51,200 --> 00:05:57,260
There aren't any more fields we need to validate, let's test the validations we created for the two
67
00:05:57,260 --> 00:05:57,890
fields.
68
00:05:58,040 --> 00:06:02,000
Switching to the browser will check the console for any errors.
69
00:06:04,500 --> 00:06:06,330
There aren't any errors so far.
70
00:06:06,540 --> 00:06:12,000
Next, I'll test the country field by default, it won't have an initial value.
71
00:06:12,300 --> 00:06:16,770
If I were to change the value to Mexico or Germany, then nothing happens.
72
00:06:16,950 --> 00:06:21,620
However, changing it to Antarctica will break the excluded rule.
73
00:06:21,810 --> 00:06:23,080
It'll output an error.
74
00:06:23,460 --> 00:06:26,080
This is the exact behavior we were looking for.
75
00:06:26,640 --> 00:06:31,410
As for the terms of service, the error doesn't appear even though it's required.
76
00:06:31,770 --> 00:06:35,520
Validation is not performed until the input has been touched.
77
00:06:35,830 --> 00:06:37,540
The default behavior is fine.
78
00:06:37,920 --> 00:06:44,790
This is because the form component will force validation on submission, will learn how that works when
79
00:06:44,790 --> 00:06:51,600
we start working on form submission for now, will test the checkbox by checking it and then uncheck
80
00:06:51,750 --> 00:06:51,960
it.
81
00:06:54,600 --> 00:07:01,910
The rule is being enforced, our form is completely validated now that we validated every input field,
82
00:07:01,920 --> 00:07:08,790
we can move on to the next step by default, validations are not performed until the input fields have
83
00:07:08,790 --> 00:07:09,570
been touched.
84
00:07:09,870 --> 00:07:14,700
In some cases, a user can accidentally skip a field and submit the form.
85
00:07:15,090 --> 00:07:18,070
Any fields that were skipped will not be validated.
86
00:07:18,240 --> 00:07:19,250
We don't want that.
87
00:07:19,650 --> 00:07:23,220
We want the validation to be performed and informed the user.
88
00:07:23,220 --> 00:07:26,870
When that happens, that's what we'll be doing in the next lecture.
89
00:07:26,910 --> 00:07:28,680
When you're ready, I'll see you there.
9320
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.