Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,090 --> 00:00:04,019
The main goal of this section is to go over form validation.
2
00:00:04,050 --> 00:00:08,070
In the next section we'll store the form data in a database.
3
00:00:08,070 --> 00:00:12,360
We'll use that data to authenticate a user into our application.
4
00:00:12,360 --> 00:00:16,230
Before we get into that, I want to prepare the login form.
5
00:00:16,260 --> 00:00:21,570
Validating the login form is the same process as validating the registration form.
6
00:00:21,570 --> 00:00:25,710
The only difference is that there are two fields instead of seven.
7
00:00:26,040 --> 00:00:29,460
This is a perfect opportunity to test what you've learned.
8
00:00:29,460 --> 00:00:33,000
You already have the knowledge to validate the login form.
9
00:00:33,000 --> 00:00:35,460
Try invalidating the form on your own.
10
00:00:35,460 --> 00:00:38,820
Get as far as you can, use any rules you like.
11
00:00:38,820 --> 00:00:43,140
Try to replicate the same behavior we had for the registration form.
12
00:00:43,140 --> 00:00:44,070
Good luck.
13
00:00:45,610 --> 00:00:46,750
Welcome back.
14
00:00:46,780 --> 00:00:49,970
If you were able to validate the form, then congrats.
15
00:00:49,990 --> 00:00:52,010
If not, that's fine as well.
16
00:00:52,030 --> 00:00:53,440
Let's do it together.
17
00:00:53,470 --> 00:00:56,440
Open the authentication component file.
18
00:00:56,440 --> 00:00:58,320
Search for the login form.
19
00:00:58,330 --> 00:01:01,320
There's a comment above it that says login form.
20
00:01:01,330 --> 00:01:05,800
Inside it we'll find a form similar to the registration form.
21
00:01:05,920 --> 00:01:10,290
There are two fields which are the email and password fields.
22
00:01:10,300 --> 00:01:12,720
We're going to validate both fields.
23
00:01:12,730 --> 00:01:14,650
We'll start with the email.
24
00:01:14,860 --> 00:01:20,500
First, we'll change the input element for the email to the the field component.
25
00:01:22,820 --> 00:01:26,750
This component will tell the validate to validate the input.
26
00:01:26,750 --> 00:01:33,700
Whenever the value changes, we'll need to assign a unique ID to the field by adding the name attribute.
27
00:01:33,710 --> 00:01:35,450
We'll set it to email.
28
00:01:37,570 --> 00:01:39,600
Next, we'll add the rules.
29
00:01:39,610 --> 00:01:42,800
We can add rules directly to the field component.
30
00:01:42,820 --> 00:01:48,190
However, I think outsourcing the rules to an object will be beneficial for readability.
31
00:01:48,220 --> 00:01:54,100
First, we'll need to change the form element and the template to the V form component.
32
00:01:56,510 --> 00:02:03,080
On this component, we can bind a property called validation schema to an object called log in schema.
33
00:02:06,610 --> 00:02:12,280
A schema is an object that represents the fields in the form with the rules we'd like to enforce on
34
00:02:12,280 --> 00:02:12,790
them.
35
00:02:12,790 --> 00:02:14,890
This object doesn't exist.
36
00:02:14,890 --> 00:02:17,730
We'll need to create it in the data function.
37
00:02:17,740 --> 00:02:19,150
Let's do that now.
38
00:02:21,490 --> 00:02:22,720
Inside the object.
39
00:02:22,720 --> 00:02:25,490
We'll add the rules for the email field.
40
00:02:25,510 --> 00:02:30,520
The two rules will enforce are the required and email rules.
41
00:02:32,670 --> 00:02:34,700
We can work on handling the errors.
42
00:02:34,710 --> 00:02:39,600
Next, it's important to provide feedback to users who are trying to log in.
43
00:02:39,630 --> 00:02:41,490
Let's go back to the template.
44
00:02:43,570 --> 00:02:47,890
Below the field component will add the error message component.
45
00:02:49,990 --> 00:02:54,730
The component should have the text read 600 class applied to it.
46
00:02:56,870 --> 00:02:59,690
This class will change the text color to red.
47
00:02:59,720 --> 00:03:02,510
We'll set the name property to email.
48
00:03:04,730 --> 00:03:10,640
The value for the name property should correspond to the value passed into the name property of the
49
00:03:10,640 --> 00:03:11,810
field component.
50
00:03:12,560 --> 00:03:15,230
We're finished working with the email field.
51
00:03:15,260 --> 00:03:18,230
Next, let's move on to the password field.
52
00:03:18,260 --> 00:03:19,980
It's the same as before.
53
00:03:20,000 --> 00:03:27,230
We'll change the input element to the field component to tell the validate to perform validation on
54
00:03:27,230 --> 00:03:28,010
the value.
55
00:03:30,400 --> 00:03:34,420
Afterward, we'll set the name property to password.
56
00:03:36,630 --> 00:03:39,420
With that set, let's render the errors.
57
00:03:39,450 --> 00:03:44,790
We're going to copy and paste the error message component we use for the email input.
58
00:03:47,030 --> 00:03:50,360
The name property will be updated to password.
59
00:03:52,600 --> 00:03:56,170
The last thing we'll do is apply some rules to the password.
60
00:03:56,200 --> 00:04:00,610
Scroll down to the schema object we created for the login form.
61
00:04:00,610 --> 00:04:03,700
We'll add the password field to the object.
62
00:04:05,860 --> 00:04:08,830
We'll use the following rules required.
63
00:04:08,830 --> 00:04:09,460
Minimum.
64
00:04:09,460 --> 00:04:10,270
Maximum.
65
00:04:10,840 --> 00:04:15,280
You'll notice I'm using the same rules we used in the registration form.
66
00:04:15,280 --> 00:04:19,329
There's not much of a reason for us to use completely different rules.
67
00:04:19,329 --> 00:04:25,000
We're not going to loop through the errors for the password because it's unnecessary to do so.
68
00:04:25,030 --> 00:04:28,300
The user will already know that their password is valid.
69
00:04:28,450 --> 00:04:31,030
We're finished validating the inputs.
70
00:04:31,030 --> 00:04:37,270
The next step is to enforce the validation in case the user attempts to submit the form without filling
71
00:04:37,270 --> 00:04:38,410
out the fields.
72
00:04:38,410 --> 00:04:44,470
We can do that by listening for the submit event on the form component for the login form.
73
00:04:44,650 --> 00:04:45,850
Back in the template.
74
00:04:45,850 --> 00:04:50,170
We'll listen for this event or the function to run will be called log in.
75
00:04:52,470 --> 00:04:55,620
The Summit event will do two things for us.
76
00:04:55,650 --> 00:05:01,030
Firstly, it'll enforce the validation before running the function we have in the expression.
77
00:05:01,050 --> 00:05:06,040
If the validation fails in any of the fields, it will not run the expression.
78
00:05:06,060 --> 00:05:10,950
Secondly, it'll prevent the page from refreshing when the form is submitted.
79
00:05:11,220 --> 00:05:15,880
We're using two form components in the authentication component.
80
00:05:15,900 --> 00:05:19,290
We don't have to worry about conflicts between the two.
81
00:05:19,320 --> 00:05:24,480
The form component will only validate field components passed into it.
82
00:05:24,630 --> 00:05:28,170
Errors in one component will not be reflected on the other.
83
00:05:28,650 --> 00:05:31,290
The log in function doesn't exist.
84
00:05:31,290 --> 00:05:32,990
We'll need to define it.
85
00:05:33,000 --> 00:05:36,720
Scroll down to the methods object and define it.
86
00:05:39,000 --> 00:05:45,450
Inside the function we're going to log the values parameter will receive from the event.
87
00:05:47,740 --> 00:05:53,230
This will help us verify that the function was executed if the validation was successful.
88
00:05:53,260 --> 00:05:57,570
We'll want to write more code, but let's make sure everything is working.
89
00:05:57,580 --> 00:06:00,280
Save your changes and switch to the browser.
90
00:06:02,570 --> 00:06:06,200
If I were to check the console, there won't be any errors.
91
00:06:06,230 --> 00:06:07,110
That's great.
92
00:06:07,130 --> 00:06:09,020
We shouldn't have any thus far.
93
00:06:09,050 --> 00:06:13,310
Next, let's try submitting the form with invalid values.
94
00:06:15,460 --> 00:06:19,050
This will break the rules because both fields are required.
95
00:06:19,060 --> 00:06:22,860
We will see the error messages appear below the inputs.
96
00:06:22,870 --> 00:06:27,190
Previously we received false as an error message.
97
00:06:27,190 --> 00:06:32,260
However, we took the time to write generic error messages for our rules.
98
00:06:32,260 --> 00:06:37,900
The messages will work in all forms regardless of where we write the form in the application.
99
00:06:37,930 --> 00:06:43,420
After verifying the errors work, let's try submitting the form with valid values.
100
00:06:45,670 --> 00:06:50,750
After submitting the form, the console will output the values from the fields.
101
00:06:50,770 --> 00:06:51,910
Fantastic.
102
00:06:51,910 --> 00:06:58,090
We can move on to the next step, which is to render alert boxes with information about the form submission
103
00:06:58,090 --> 00:06:58,990
progress.
104
00:06:59,260 --> 00:07:04,990
Before we get to that, there is one change I want to make to the authentication component.
105
00:07:05,020 --> 00:07:08,700
If you look at the file in the editor, it's getting quite large.
106
00:07:08,710 --> 00:07:13,630
I think it would be a good idea to separate some of the logic into its own component.
107
00:07:13,660 --> 00:07:17,680
This separation of logic will make things easier to manage.
108
00:07:17,770 --> 00:07:19,180
Here's what we'll do.
109
00:07:19,210 --> 00:07:26,290
We'll create two components one for the login form and another for the registration form inside the
110
00:07:26,290 --> 00:07:27,820
components directory.
111
00:07:27,850 --> 00:07:33,700
Create two files called login form view and register form view.
112
00:07:36,080 --> 00:07:40,670
Then scaffold both files with the template and script blocks.
113
00:07:43,130 --> 00:07:46,460
I'm using Pascal casing for both file names.
114
00:07:46,500 --> 00:07:51,920
View recommends either using Pascal casing or kebab casing for this course.
115
00:07:51,920 --> 00:07:56,060
We're going to stick to using Pascal casing for our file names.
116
00:07:56,300 --> 00:08:02,090
The next step is to start moving the forms to their respective components, starting with the log in
117
00:08:02,090 --> 00:08:07,820
component, search for the log in form in the template block of the authentication component.
118
00:08:07,850 --> 00:08:11,300
We're going to cut the form component completely.
119
00:08:13,590 --> 00:08:19,860
Pasted into the template block of the login form component, you may need to format it after pasting
120
00:08:19,860 --> 00:08:20,400
it in.
121
00:08:22,690 --> 00:08:29,600
Next, go to the register form in the authentication component file cut and paste the form component.
122
00:08:29,620 --> 00:08:32,289
We're going to grab the alert element to.
123
00:08:34,520 --> 00:08:38,720
We'll paste it into the register form components template block.
124
00:08:40,900 --> 00:08:42,960
The templates have been transferred.
125
00:08:42,970 --> 00:08:45,950
They won't work until the data and methods are present.
126
00:08:45,970 --> 00:08:47,450
We'll move them next.
127
00:08:47,470 --> 00:08:52,210
Inside the authentication component, we're going to copy the data function.
128
00:08:54,240 --> 00:08:59,790
Since the authentication component doesn't need the data anymore, we're going to remove all the properties
129
00:08:59,790 --> 00:09:01,530
except the tabs property.
130
00:09:03,840 --> 00:09:09,630
Go over to the register component, we'll paste in the data function to the components configuration
131
00:09:09,630 --> 00:09:12,090
object without the tabs property.
132
00:09:14,130 --> 00:09:16,320
Next, we'll create the methods.
133
00:09:16,320 --> 00:09:17,280
Objects.
134
00:09:20,410 --> 00:09:26,800
Then we'll cut and paste the register function from the authentication component to the register component.
135
00:09:29,110 --> 00:09:31,350
The Register component is ready.
136
00:09:31,360 --> 00:09:35,290
The login component still needs its data and methods.
137
00:09:35,290 --> 00:09:39,760
Switch to the login component file in the exported object.
138
00:09:39,760 --> 00:09:43,210
Define the data function and the methods object.
139
00:09:47,070 --> 00:09:53,220
The log and components data can be found inside the register component since we copied over the entire
140
00:09:53,220 --> 00:10:00,030
data object over to it in the data function of the register component, search for the log in schema
141
00:10:00,030 --> 00:10:00,920
object.
142
00:10:00,930 --> 00:10:05,070
This is the only data property we created for the log in form.
143
00:10:05,070 --> 00:10:09,660
Cut and paste it into the data function for the login component.
144
00:10:11,950 --> 00:10:18,760
In the authentication component file, cut the login function, paste it into the methods object of
145
00:10:18,760 --> 00:10:20,080
the login component.
146
00:10:22,340 --> 00:10:28,370
One last thing before we use the components, we should assign them names to make it easier to debug
147
00:10:28,370 --> 00:10:31,310
the application in the login component.
148
00:10:31,310 --> 00:10:34,610
Set the name property to login form.
149
00:10:36,810 --> 00:10:38,820
For the register component.
150
00:10:38,820 --> 00:10:42,150
Set the name property to register form.
151
00:10:47,120 --> 00:10:48,200
That'll do it.
152
00:10:48,230 --> 00:10:53,120
The very last step is to register the components in the authentication component.
153
00:10:53,150 --> 00:10:58,060
We're going to register them locally because the forms won't be used anywhere else.
154
00:10:58,070 --> 00:10:59,150
They don't need to be.
155
00:10:59,150 --> 00:11:06,560
Global components inside the authentication component at the top of the script block will import the
156
00:11:06,560 --> 00:11:08,720
form components we created.
157
00:11:13,510 --> 00:11:18,080
I recommend prefix ing them with the word app to prevent name collisions.
158
00:11:18,100 --> 00:11:20,200
Not required but recommended.
159
00:11:22,550 --> 00:11:27,290
Inside the exported object, we'll create the components object.
160
00:11:29,590 --> 00:11:32,440
Register both components in the object.
161
00:11:34,700 --> 00:11:38,570
By registering the components they are available in the template.
162
00:11:38,600 --> 00:11:42,290
We'll want to load the forms in their respective tabs.
163
00:11:42,290 --> 00:11:47,030
Scrolling up will load the log and form component under the tabs.
164
00:11:49,170 --> 00:11:52,860
Afterward, we'll load the register form components.
165
00:11:55,100 --> 00:11:59,010
The conditional directives will need to be added for toggling the forms.
166
00:11:59,030 --> 00:12:03,600
We're going to use the V if directives instead of the V show directive.
167
00:12:03,620 --> 00:12:07,640
They're going to be much more efficient on the log and form component.
168
00:12:07,640 --> 00:12:14,270
We're going to add the V if directive the condition will be the following tab equals equals equals.
169
00:12:14,270 --> 00:12:15,140
Log in.
170
00:12:17,230 --> 00:12:21,490
We'll apply the directive on the register form component.
171
00:12:23,560 --> 00:12:30,910
Next in the login and register components will remove the V show directives on the form components.
172
00:12:33,190 --> 00:12:36,640
That was a lot of refactoring to do, but well worth it.
173
00:12:36,670 --> 00:12:39,940
This refactor will make things easier to manage.
174
00:12:39,970 --> 00:12:41,790
I know I flew right through that.
175
00:12:41,800 --> 00:12:44,680
If any of the steps we took didn't make sense.
176
00:12:44,710 --> 00:12:49,120
I recommend reviewing any of the previous lectures for more information.
177
00:12:49,420 --> 00:12:53,770
All right, let's quickly try testing the forms in the browser.
178
00:12:53,770 --> 00:12:56,170
Inspect the console for any errors.
179
00:12:56,170 --> 00:12:58,030
There shouldn't be any so far.
180
00:13:00,270 --> 00:13:03,450
After confirming there aren't errors in the console.
181
00:13:03,480 --> 00:13:06,570
Try testing the login form with valid input.
182
00:13:08,860 --> 00:13:09,940
In the console.
183
00:13:09,940 --> 00:13:13,520
We will see the message logged from the login function.
184
00:13:13,540 --> 00:13:14,660
This is great.
185
00:13:14,680 --> 00:13:18,170
We were able to move the forms into separate components.
186
00:13:18,190 --> 00:13:21,940
None of the original functionality we had before was lost.
187
00:13:21,970 --> 00:13:23,420
We are not finished yet.
188
00:13:23,440 --> 00:13:26,290
The last thing we'll want to add is the alerts.
189
00:13:26,710 --> 00:13:30,800
The alerts inform the user about the status of their submission.
190
00:13:30,820 --> 00:13:33,450
We've already created the logic for this.
191
00:13:33,460 --> 00:13:37,900
We can copy a lot of the code we've already written with some modifications.
192
00:13:38,020 --> 00:13:42,700
Back in, the editor opened the register form component file.
193
00:13:42,700 --> 00:13:50,470
In the data function, we're going to make a copy of the IND submission show Alert, Alert Variant and
194
00:13:50,470 --> 00:13:52,420
alert message properties.
195
00:13:54,570 --> 00:14:00,960
Then we'll paste the data properties into the data function of the login component.
196
00:14:03,390 --> 00:14:07,440
We're making copies because the data properties will mainly be the same.
197
00:14:07,440 --> 00:14:09,820
However, the names don't make sense.
198
00:14:09,840 --> 00:14:15,840
We're going to change the word registration with log in in each of the data property names.
199
00:14:18,220 --> 00:14:20,890
I'll recap what each property is for.
200
00:14:20,920 --> 00:14:27,100
The login in submission property will be used to disable the button while the form is submitting the
201
00:14:27,100 --> 00:14:27,680
data.
202
00:14:27,700 --> 00:14:30,090
It's to prevent excessive requests.
203
00:14:30,100 --> 00:14:35,470
The login show alert property will be used to toggle the alerts visibility.
204
00:14:35,620 --> 00:14:39,910
The alert variant property will change the colour of the alert.
205
00:14:40,060 --> 00:14:45,280
Lastly, the alert message property will be the message inside the alert.
206
00:14:45,460 --> 00:14:48,510
The message property will need to be updated.
207
00:14:48,520 --> 00:14:50,110
Change it to the following.
208
00:14:50,110 --> 00:14:51,010
Please wait.
209
00:14:51,010 --> 00:14:52,360
We are logging you in.
210
00:14:54,670 --> 00:14:57,680
We can begin adding the alert to the template.
211
00:14:57,700 --> 00:15:00,420
Scroll to the top of the template block.
212
00:15:00,430 --> 00:15:05,470
We'll add the alert above the form component above the form component.
213
00:15:05,470 --> 00:15:14,650
Add a div tag with the classes text white text center font bold p for mv for.
214
00:15:19,150 --> 00:15:23,280
Will add a V if directive to toggle the elements visibility.
215
00:15:23,290 --> 00:15:27,190
The condition will be the log and show alert data property.
216
00:15:29,390 --> 00:15:35,210
Next we'll bind another class attribute to the login alert variant data property.
217
00:15:37,150 --> 00:15:41,470
Inside the element and the expression log in alert message.
218
00:15:43,450 --> 00:15:47,620
We'll need to disable the button while the form is submitting the request.
219
00:15:47,650 --> 00:15:51,630
You'll find the submit button at the bottom of the form element.
220
00:15:51,640 --> 00:15:55,210
Bind the disabled attribute to the expression.
221
00:15:55,210 --> 00:15:56,980
Log in in submission.
222
00:15:59,310 --> 00:16:01,490
By default, this will be false.
223
00:16:01,500 --> 00:16:03,360
The button will remain enabled.
224
00:16:03,360 --> 00:16:06,930
We'll need to toggle the properties through the login function.
225
00:16:06,930 --> 00:16:08,970
Before we submit the request.
226
00:16:08,970 --> 00:16:15,000
We'll need to reset the data properties if the user is submitting the form again, we'll set the in
227
00:16:15,000 --> 00:16:18,540
submission and show alert properties to true.
228
00:16:20,550 --> 00:16:25,740
The alert variant data property will be set to BG Blue 500.
229
00:16:27,930 --> 00:16:31,470
We'll set the message property to its initial value.
230
00:16:37,120 --> 00:16:40,250
We'll cover submitting the request in the next section.
231
00:16:40,270 --> 00:16:46,430
In the meantime, we'll create a dummy response to let the user know their login was a success.
232
00:16:46,450 --> 00:16:50,680
Set the variant property to be green 500.
233
00:16:50,890 --> 00:16:54,580
This value will change the alert box to the color green.
234
00:16:56,850 --> 00:17:02,030
Lastly, we'll update the message data property to the following success.
235
00:17:02,040 --> 00:17:03,360
You are now logged in.
236
00:17:05,770 --> 00:17:10,970
The process for validating the login form is the same as the registration form.
237
00:17:10,990 --> 00:17:14,780
This is the benefit of using the validate library.
238
00:17:14,800 --> 00:17:17,390
It makes the process very streamlined.
239
00:17:17,410 --> 00:17:19,960
Let's give things one last test.
240
00:17:19,990 --> 00:17:23,410
I recommend refreshing the browser for a fresh start.
241
00:17:23,440 --> 00:17:26,440
Try filling out the form with valid values.
242
00:17:28,569 --> 00:17:31,060
We will receive a success message.
243
00:17:31,090 --> 00:17:31,960
That's great.
244
00:17:31,990 --> 00:17:37,490
The button is disabled since we didn't toggle the in submission property back to false.
245
00:17:37,510 --> 00:17:41,470
We don't need to do that since the user has logged in successfully.
246
00:17:41,830 --> 00:17:42,890
We're finished.
247
00:17:42,910 --> 00:17:45,010
I know I breezed right through that.
248
00:17:45,310 --> 00:17:49,590
This was meant as an exercise to review everything we did.
249
00:17:49,600 --> 00:17:52,150
Both forms are properly validated.
250
00:17:52,150 --> 00:17:57,210
We can move on to the next step, which is to register the user and authenticate them.
251
00:17:57,220 --> 00:17:59,590
We'll cover that in the next section.
23506
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.