1 00:00:00,120 --> 00:00:05,490 In this lecture, we will begin validating the password fields by checking if they match. 2 00:00:05,790 --> 00:00:12,840 As we discussed before, we will expect this validator to be applied to a form group instead of a control. 3 00:00:13,230 --> 00:00:19,290 The validator should have access to the fields that should have matching values to get started. 4 00:00:19,440 --> 00:00:22,470 Let's open the register validators file. 5 00:00:25,030 --> 00:00:29,080 In this class, we will add a static method called Match. 6 00:00:31,570 --> 00:00:37,180 If you're not familiar with the static method, let me quickly run down what this feature is for. 7 00:00:37,510 --> 00:00:44,650 We can think of classes as blueprints for objects whenever we want to invoke a method or access a property. 8 00:00:44,830 --> 00:00:47,950 We need to create an instance out of the class. 9 00:00:48,310 --> 00:00:50,590 I'll add a comment below for clarity. 10 00:00:51,160 --> 00:00:55,180 Let's say the match method did not have the static keyword. 11 00:00:55,450 --> 00:01:01,690 If we want to call this method, a new instance of the Register of Validators class must be created. 12 00:01:02,080 --> 00:01:05,890 After creating the instance, we will be able to call this method. 13 00:01:06,520 --> 00:01:09,880 Static methods are different if a method is static. 14 00:01:10,000 --> 00:01:14,140 We can directly call the method without creating a new instance. 15 00:01:16,730 --> 00:01:20,450 Static methods are beneficial for creating utility methods. 16 00:01:20,720 --> 00:01:27,050 If we don't need to maintain an object state, converting a method into a static method will result 17 00:01:27,050 --> 00:01:28,370 in writing less code. 18 00:01:28,730 --> 00:01:35,570 Alternatively, we could define a function instead of writing a static method inside a class. 19 00:01:35,910 --> 00:01:41,150 However, you may want to define a static method for grouping the method in a class. 20 00:01:41,780 --> 00:01:45,010 The name of our class is called register validators. 21 00:01:45,290 --> 00:01:51,860 If we want to define multiple validators for the registration form, we can define them under a single 22 00:01:51,860 --> 00:01:53,480 namespace for clarity. 23 00:01:53,870 --> 00:01:59,360 It's an optional step, but I think it's worth grouping our validators in a single class. 24 00:01:59,750 --> 00:02:04,670 Adding the static keyword to a method will reduce the amount of code we'll have to write. 25 00:02:05,300 --> 00:02:08,210 There are some limitations with static methods. 26 00:02:08,539 --> 00:02:12,410 They do not have access to an object's properties or methods. 27 00:02:12,710 --> 00:02:19,490 For example, let's add a property to the class called test with an initial value of five. 28 00:02:22,120 --> 00:02:26,260 Inside the match method, we will log the test property. 29 00:02:29,040 --> 00:02:30,840 TypeScript will throw an error. 30 00:02:31,230 --> 00:02:34,890 It says the test property is not defined in the class. 31 00:02:35,100 --> 00:02:42,750 However, clearly it is the reason it's not detecting the test property is because static methods don't 32 00:02:42,750 --> 00:02:49,290 have access to an object's properties or methods since we're not creating an instance out of the class. 33 00:02:49,530 --> 00:02:53,910 The methods and properties are never instantiated using the. 34 00:02:53,910 --> 00:02:57,450 This key word is prohibited within a static method. 35 00:02:58,260 --> 00:03:02,160 If we were to remove the static method, the error would go away. 36 00:03:02,580 --> 00:03:04,830 Always keep this limitation in mind. 37 00:03:05,130 --> 00:03:10,620 Static methods are great for creating utility methods, but are limited with their scope. 38 00:03:11,040 --> 00:03:13,560 I'll add the static keyword back in. 39 00:03:13,980 --> 00:03:17,880 Let's move on to working on the validator in the following lecture.