In this lecture, we will begin validating the password fields by checking if they match.
As we discussed before, we will expect this validator to be applied to a form group instead of a control.
The validator should have access to the fields that should have matching values to get started.
Let's open the register validators file.
In this class, we will add a static method called Match.
If you're not familiar with the static method, let me quickly run down what this feature is for.
We can think of classes as blueprints for objects whenever we want to invoke a method or access a property.
We need to create an instance out of the class.
I'll add a comment below for clarity.
Let's say the match method did not have the static keyword.
If we want to call this method, a new instance of the Register of Validators class must be created.
After creating the instance, we will be able to call this method.
Static methods are different if a method is static.
We can directly call the method without creating a new instance.
Static methods are beneficial for creating utility methods.
If we don't need to maintain an object state, converting a method into a static method will result
in writing less code.
Alternatively, we could define a function instead of writing a static method inside a class.
However, you may want to define a static method for grouping the method in a class.
The name of our class is called register validators.
If we want to define multiple validators for the registration form, we can define them under a single
namespace for clarity.
It's an optional step, but I think it's worth grouping our validators in a single class.
Adding the static keyword to a method will reduce the amount of code we'll have to write.
There are some limitations with static methods.
They do not have access to an object's properties or methods.
For example, let's add a property to the class called test with an initial value of five.
Inside the match method, we will log the test property.
TypeScript will throw an error.
It says the test property is not defined in the class.
However, clearly it is the reason it's not detecting the test property is because static methods don't
have access to an object's properties or methods since we're not creating an instance out of the class.
The methods and properties are never instantiated using the.
This key word is prohibited within a static method.
If we were to remove the static method, the error would go away.
Always keep this limitation in mind.
Static methods are great for creating utility methods, but are limited with their scope.
I'll add the static keyword back in.
Let's move on to working on the validator in the following lecture.