Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,150 --> 00:00:04,590
Up until now, we've been stuck with the same error message for every rule.
2
00:00:04,620 --> 00:00:10,680
The reason we get the same message is because of how rules are defined in the validation file.
3
00:00:10,710 --> 00:00:14,530
We're using the defined rule function to create our rules.
4
00:00:14,550 --> 00:00:17,940
The second argument to the function is a pure function.
5
00:00:18,180 --> 00:00:21,450
The pure function can return one of two values.
6
00:00:21,480 --> 00:00:24,720
It can return true if the validation passes.
7
00:00:24,720 --> 00:00:26,460
Or it can return a string.
8
00:00:26,490 --> 00:00:29,980
If it returns a string, then the validation fails.
9
00:00:30,000 --> 00:00:34,770
The string must be a message to tell the user why the validation failed.
10
00:00:34,800 --> 00:00:39,560
It's important to understand this we aren't creating our own rules.
11
00:00:39,570 --> 00:00:44,110
Instead, we're using rules from the validate rules package.
12
00:00:44,130 --> 00:00:47,130
The functions we're importing don't return a string.
13
00:00:47,130 --> 00:00:51,760
If a validation fails, they're returning a false boolean value.
14
00:00:51,810 --> 00:00:55,350
The validate provides a way for overriding a message.
15
00:00:55,380 --> 00:00:58,290
There are some limitations to what we can override.
16
00:00:58,320 --> 00:01:03,060
Firstly, we can only overwrite messages from global rules.
17
00:01:03,090 --> 00:01:06,540
Keep in mind we're allowed to define rules locally.
18
00:01:06,570 --> 00:01:10,110
A local rules error message cannot be overridden.
19
00:01:10,440 --> 00:01:16,650
The second thing to understand is that this will only work for functions that return a false boolean
20
00:01:16,650 --> 00:01:17,400
value.
21
00:01:17,610 --> 00:01:22,890
If the functions from the package returned in actual string, we wouldn't be able to override those
22
00:01:22,890 --> 00:01:23,810
messages.
23
00:01:23,820 --> 00:01:27,080
Luckily they do, so we'll be able to override them.
24
00:01:27,090 --> 00:01:28,920
Let's explore how that's done.
25
00:01:29,250 --> 00:01:34,590
We're going to create the custom messages in the validation file at the top.
26
00:01:34,590 --> 00:01:38,100
We'll update the import statement for the configure function.
27
00:01:40,320 --> 00:01:46,200
The configure function can be used to configure the default behavior of the validation library.
28
00:01:46,230 --> 00:01:52,700
We'll be able to override the default messages from our global validators inside the install method.
29
00:01:52,710 --> 00:01:54,870
We'll call the configure function.
30
00:01:57,100 --> 00:02:02,250
This function has one argument, which is an object with the configuration options.
31
00:02:02,260 --> 00:02:06,580
There's one option we'll want to add called generate message.
32
00:02:08,840 --> 00:02:14,810
This option will be an arrow function where we'll have access to a parameter called context.
33
00:02:17,100 --> 00:02:22,860
The generate message function will be called whenever a global validator function returns false.
34
00:02:22,890 --> 00:02:27,370
This function will handle all global validators inside the function.
35
00:02:27,390 --> 00:02:32,610
We must return a string that will represent the message we want to output to the document.
36
00:02:32,640 --> 00:02:38,200
The context parameter is an object information about the field being validated.
37
00:02:38,220 --> 00:02:43,710
We'll have access to the name, value and rule being broken by the fields.
38
00:02:44,040 --> 00:02:51,000
We can use it to help us output different error messages based on the rule being broken inside the function.
39
00:02:51,030 --> 00:02:56,250
We're going to write a message for every global rule we defined in the install method.
40
00:02:56,280 --> 00:02:58,990
We'll start from the top and work our way down.
41
00:02:59,010 --> 00:03:01,870
We'll declare a variable called messages.
42
00:03:01,890 --> 00:03:03,840
It'll be set to an object.
43
00:03:06,250 --> 00:03:09,010
We're going to add a property for every rule.
44
00:03:09,040 --> 00:03:12,970
The name of the property will correspond to the name of the rule.
45
00:03:13,000 --> 00:03:18,610
It's crucial we use the same name because it's how we'll be able to search for the rule when we need
46
00:03:18,610 --> 00:03:19,600
to output it.
47
00:03:19,630 --> 00:03:21,760
You'll see what that looks like in a moment.
48
00:03:22,000 --> 00:03:24,730
The first rule is called required.
49
00:03:24,760 --> 00:03:26,970
Its value will be the following.
50
00:03:26,980 --> 00:03:30,850
The field context field is required.
51
00:03:32,940 --> 00:03:38,940
From the context object, we can access the name of the field via the field property.
52
00:03:39,120 --> 00:03:42,920
We're using a string template for inserting the name into the string.
53
00:03:42,930 --> 00:03:44,630
We'll keep moving forward.
54
00:03:44,640 --> 00:03:46,720
The next rule is called Min.
55
00:03:46,740 --> 00:03:52,200
Its message will be the following The field context field is too short.
56
00:03:54,500 --> 00:03:56,550
Then we have the max rule.
57
00:03:56,570 --> 00:04:01,940
The message will be the following The field context field is too long.
58
00:04:04,150 --> 00:04:07,420
Subsequently we have the alpha spaces rule.
59
00:04:07,450 --> 00:04:14,170
The message will be the following The field context field may only contain alphabetic characters and
60
00:04:14,170 --> 00:04:15,100
spaces.
61
00:04:20,769 --> 00:04:23,280
Afterward we have the email rule.
62
00:04:23,290 --> 00:04:29,200
The message will be the following The field context field must be a valid email.
63
00:04:31,610 --> 00:04:34,910
Up next, we have the min value rule.
64
00:04:34,940 --> 00:04:36,890
The message will be the following.
65
00:04:36,890 --> 00:04:40,070
The field context field is too low.
66
00:04:42,380 --> 00:04:45,690
Similarly, we have the max value rule.
67
00:04:45,710 --> 00:04:50,960
The message will be the following The field context field is too high.
68
00:04:53,230 --> 00:04:55,270
There are two more rules left.
69
00:04:55,300 --> 00:04:58,780
We're going to approach these two slightly differently.
70
00:04:58,810 --> 00:05:04,370
The excluded rule will check if the input does not match the list of values.
71
00:05:04,390 --> 00:05:09,100
However, the excluded rule can be used for other scenarios.
72
00:05:09,100 --> 00:05:12,820
We have to choose between a generic or specific message.
73
00:05:12,850 --> 00:05:16,150
However, there is a solution where we can have both.
74
00:05:16,420 --> 00:05:19,910
We're going to add the excluded rule to the object.
75
00:05:19,930 --> 00:05:21,760
The message will be the following.
76
00:05:21,970 --> 00:05:26,470
You are not allowed to use this value for the field context field.
77
00:05:28,690 --> 00:05:34,280
This message works great, but we want to give a more specific message in the registration form.
78
00:05:34,300 --> 00:05:38,920
We want to tell the user that they're not allowed to sign up for a certain location.
79
00:05:38,950 --> 00:05:45,640
One way we can add messages for specific inputs is by creating a rule specifically for the fields.
80
00:05:45,670 --> 00:05:47,290
Let me show you what I mean.
81
00:05:47,740 --> 00:05:50,770
We're going to make a copy of the excluded rule.
82
00:05:53,000 --> 00:05:55,640
The name will be country excluded.
83
00:05:57,810 --> 00:06:01,180
We have two rules that do the exact same thing.
84
00:06:01,200 --> 00:06:03,550
However, they have different names.
85
00:06:03,570 --> 00:06:09,720
This allows us to create a message for generic scenarios and another for specific inputs.
86
00:06:09,720 --> 00:06:13,850
Back in the messages object, we can add this rule to the list.
87
00:06:13,860 --> 00:06:16,200
We'll set the message to the following.
88
00:06:16,230 --> 00:06:20,370
Due to restrictions, we do not accept users from this location.
89
00:06:24,440 --> 00:06:25,060
Great.
90
00:06:25,070 --> 00:06:27,900
We have a special message for the country fields.
91
00:06:27,920 --> 00:06:33,470
We're going to update the name of the confirmed rule to passwords mismatch.
92
00:06:35,680 --> 00:06:40,350
We are going to create a copy because we'll only be using this rule once.
93
00:06:40,360 --> 00:06:44,950
We don't need to create a generic message inside the messages object.
94
00:06:44,950 --> 00:06:47,720
We'll add the passwords mismatch rule.
95
00:06:47,740 --> 00:06:51,280
The message will be the following The passwords don't match.
96
00:06:53,500 --> 00:06:55,870
There is one last rule will add.
97
00:06:55,900 --> 00:06:59,900
The default message for the terms of service doesn't sit well with me.
98
00:06:59,920 --> 00:07:03,110
The terms of service are using the required rule.
99
00:07:03,130 --> 00:07:07,460
We want to tell the user directly they must agree to the terms of service.
100
00:07:07,480 --> 00:07:14,320
We'll make a copy of the required rule because we'll need one copy for generic scenarios and another
101
00:07:14,320 --> 00:07:16,000
for the terms of service.
102
00:07:20,250 --> 00:07:26,670
The name of the copy will be to iOS and the messages object will add the following message For this
103
00:07:26,670 --> 00:07:30,180
new rule you must accept the terms of service.
104
00:07:34,630 --> 00:07:37,520
We have two rules that do the same thing.
105
00:07:37,540 --> 00:07:40,300
They require the user to input a value.
106
00:07:40,330 --> 00:07:47,800
The difference being is the message the required rule can be used for regular inputs, while the toss
107
00:07:47,800 --> 00:07:51,610
rule is specifically for the terms of service checkbox.
108
00:07:52,150 --> 00:07:54,270
We're finished writing the rules.
109
00:07:54,280 --> 00:07:58,840
The next thing we'll do is tell validate which message to use.
110
00:07:58,840 --> 00:08:01,540
We'll declare a variable called message.
111
00:08:01,570 --> 00:08:05,470
It's now you will be a ternary operator in the condition.
112
00:08:05,470 --> 00:08:10,930
We'll check if the messages context dot rule name exists.
113
00:08:13,740 --> 00:08:18,390
The rule name property refers to the name of the rule that was broken.
114
00:08:18,420 --> 00:08:21,600
We want to check if a message for this rule exists.
115
00:08:21,630 --> 00:08:24,900
If it does, we'll set the message variable to it.
116
00:08:27,290 --> 00:08:31,220
Otherwise we'll have a generic message which will be the following.
117
00:08:31,340 --> 00:08:34,610
The field context field is invalid.
118
00:08:42,100 --> 00:08:44,710
Afterward, we'll return the variable.
119
00:08:46,870 --> 00:08:50,430
We're not going to add any more custom error messages.
120
00:08:50,440 --> 00:08:52,720
There's one last thing we need to do.
121
00:08:52,750 --> 00:08:58,030
We'll need to update our form to use the custom rules we created in the object.
122
00:08:58,030 --> 00:09:00,840
Switch over to the authentication component.
123
00:09:00,850 --> 00:09:04,000
We'll need to update the schema data property.
124
00:09:06,290 --> 00:09:13,040
For the password confirm field will change the rule confirmed to passwords mismatch.
125
00:09:15,200 --> 00:09:22,040
Next the country field will have its excluded rule changed to the country excluded rule.
126
00:09:24,310 --> 00:09:30,100
Lastly, the toss field will use the toss rule instead of the required rule.
127
00:09:32,360 --> 00:09:34,070
Let's give things a test.
128
00:09:34,100 --> 00:09:35,840
Open the app in the browser.
129
00:09:37,400 --> 00:09:40,280
First we'll test the country selection.
130
00:09:40,310 --> 00:09:42,330
I'll select Antarctica.
131
00:09:42,350 --> 00:09:46,400
The error will tell us that we're not allowed to register from this location.
132
00:09:46,430 --> 00:09:49,010
This is a much better message than before.
133
00:09:49,220 --> 00:09:53,220
Next, let's test the terms of service checkbox.
134
00:09:53,240 --> 00:09:55,970
If we were to one, check the box after checking it.
135
00:09:56,000 --> 00:10:00,770
We'd see the custom error message, even though the name of the rule is different.
136
00:10:00,800 --> 00:10:05,840
It'll still work because the configuration is the same as the required rule.
137
00:10:06,020 --> 00:10:10,850
Lastly, let's test any of the other fields with the required rule.
138
00:10:13,030 --> 00:10:17,280
We've successfully created error messages for the other fields.
139
00:10:17,290 --> 00:10:23,410
One of the most important things I want you to be aware of is how we handle generic and non generic
140
00:10:23,410 --> 00:10:24,400
messages.
141
00:10:24,430 --> 00:10:28,030
We had copies of the same rules under different names.
142
00:10:28,060 --> 00:10:33,550
This allowed us to maintain generic messages while also handling special cases.
13648
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.