Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:03,719
In this lecture, we're going to learn how to define rules.
2
00:00:03,750 --> 00:00:06,870
A rule is a function that will take in the input value.
3
00:00:06,900 --> 00:00:12,830
The value returned by the function will let the validation library know if the input is valid.
4
00:00:12,840 --> 00:00:14,520
Let's take a closer look.
5
00:00:14,700 --> 00:00:19,650
By default, the validate does not assume what requirements and input must meet.
6
00:00:19,650 --> 00:00:20,640
To be valid.
7
00:00:20,640 --> 00:00:24,480
We need to tell it exactly what rules we want to enforce.
8
00:00:24,480 --> 00:00:29,880
Not only that, but we also need to tell it what rules will exist in our application.
9
00:00:30,120 --> 00:00:33,600
Validate does not load rules into our application.
10
00:00:33,600 --> 00:00:36,150
This keeps the build small and lean.
11
00:00:36,420 --> 00:00:39,870
We need to manually import them if we want to use them.
12
00:00:39,900 --> 00:00:43,140
Alternatively, we can define custom rules.
13
00:00:43,140 --> 00:00:48,300
I find that the rules that come bundled with validate will cover most scenarios.
14
00:00:48,480 --> 00:00:50,220
Let's import some rules.
15
00:00:50,220 --> 00:00:55,680
In the resource section of this lecture, I provide a link to a list of available rules.
16
00:00:57,860 --> 00:01:03,540
This page goes over how you can use the rules available in the V validate library.
17
00:01:03,560 --> 00:01:05,880
We'll go over how to use them soon.
18
00:01:05,900 --> 00:01:09,050
Scroll down to the section of available rules.
19
00:01:09,200 --> 00:01:16,190
There are over 20 available rules we can test for things like a credit card number, numeric and URL
20
00:01:16,190 --> 00:01:17,000
values.
21
00:01:17,030 --> 00:01:21,110
The first rule will be using is called the required rule.
22
00:01:21,140 --> 00:01:24,440
This rule will make sure the input has a value.
23
00:01:24,500 --> 00:01:27,860
We want to make it a requirement for the input to be filled.
24
00:01:27,890 --> 00:01:30,230
This is the rule we'll want to add.
25
00:01:30,500 --> 00:01:33,630
The first thing we'll need to do is install the rules.
26
00:01:33,650 --> 00:01:37,490
The official rules are installable via a separate package.
27
00:01:37,520 --> 00:01:39,730
Let's switch over to the command line.
28
00:01:39,740 --> 00:01:42,120
If you have the server running, turn it off.
29
00:01:42,140 --> 00:01:44,600
After doing so, run the following command.
30
00:01:44,600 --> 00:01:47,090
NPM install at V.
31
00:01:47,120 --> 00:01:49,390
Validate slash rules.
32
00:01:51,280 --> 00:01:53,650
The rules have been installed successfully.
33
00:01:53,680 --> 00:01:56,550
Let's go back to our editor to start using them.
34
00:01:56,560 --> 00:01:59,510
The next thing we'll be doing is register the rule.
35
00:01:59,530 --> 00:02:06,010
Rules can be registered locally or globally, will be performing validation on multiple forms.
36
00:02:06,010 --> 00:02:12,040
In our application, it would be better to register the rules globally so that they're readily available.
37
00:02:12,160 --> 00:02:14,080
Switch over to your editor.
38
00:02:14,990 --> 00:02:22,670
At the top of the validation file will import the required rule from the the validate rules package.
39
00:02:26,340 --> 00:02:27,960
Rules are named spaced.
40
00:02:27,990 --> 00:02:32,130
We must structure the object to grab a specific rule.
41
00:02:32,160 --> 00:02:37,550
The next thing we'll do is update the import statement from the the validate package.
42
00:02:37,560 --> 00:02:40,830
We'll want to import a function called define rule.
43
00:02:43,060 --> 00:02:48,040
The defined rule function allows us to register a rule globally.
44
00:02:48,070 --> 00:02:55,240
The rule we register will be available to every validation form we create in the install function we're
45
00:02:55,240 --> 00:02:57,940
going to call the defined rule function.
46
00:03:00,410 --> 00:03:02,980
The first argument is the name of the rule.
47
00:03:02,990 --> 00:03:04,980
You can call this whatever you want.
48
00:03:05,000 --> 00:03:08,900
You don't have to use the same names listed on the documentation.
49
00:03:08,930 --> 00:03:16,550
The first rule will want to create is the required rule for consistency reasons we'll call it required.
50
00:03:18,950 --> 00:03:22,520
The second argument is a function that will perform the validation.
51
00:03:22,520 --> 00:03:28,910
We don't need to define a function because we are importing the required function from the rules package.
52
00:03:28,940 --> 00:03:31,960
Every rule we're importing is already a function.
53
00:03:31,970 --> 00:03:34,730
We can pass it in as the second arguments.
54
00:03:37,000 --> 00:03:42,070
By using the defined rule function, we've successfully registered a new rule.
55
00:03:42,100 --> 00:03:45,550
This rule will be available to every validation form.
56
00:03:45,550 --> 00:03:48,250
Let's start applying rules in the next lecture.
5327
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.