- Overview
- Transcript
5.8 Creating Custom Validators for Reactive Forms
In this lesson you'll learn how to create a custom validator that we can use when building a reactive form.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
5.8 Creating Custom Validators for Reactive Forms
Hi, folks. In this lesson we're going to see how we can create a custom validator that we can use with a reactive form. The difference between creating a validator for a reactive form compared to creating a validator for a template driven form. Is that with a reactive form, we don't have to wrap the validator in a directive, and that means that our code is a little bit smaller and a little bit simpler, and that can only be a good thing, right? In the last lesson, we created a directive-based validator that we added to an elements within a template. We could use this directive on the input in the template for our reactive form if we wanted to, And it would work in exactly the same way. So we could just add a new error message for this particular error below the existing one. So notice that we used another convenience method here, the get error method, which allows us to get the value of a specific named error. And this helps to avoid interacting with complex nested object structures, and avoids template errors in the case where the error objects does not yet exist. So let's just go back to the browser and see if this works. And we can see that it does. So, we can use a directive-based validator in a reactive form if we want to, no problem at all. But the point that I wanna make it, is that we can create a pure validator which we can use for the reactive form. But this won't work with a template-driven form. So we can use a validating directive in both a template-driven form and a reactive form, but we can only use a pure validator in a reactive form. So we're going to create a custom validator in this lesson, and that's gonna be for the feedback form which as we know is a reactive form. So for this one, let's validate that the message that gets entered into the text area isn't in all capitals. If it is, the sender is probably quite angry and we don't wanna receive messages from angry people. We can't use the CLI to generate a validator yet, unfortunately, so we'll just have to create this file ourselves. Let's start by creating an underscore validators folder. Like most of the other things that we've created, validators should be super generic and we could use them in many places through our app. So they don't belong to any particular component or feature. So let's call our validator, not-or-caps.validator.ts. So first of all we'll need to import some things from the Angular. We only need a couple of things here. We need the abstract control class, just for type information really, and the Validator Fn interface, again mostly just for typing information. So now we can add the validator itself, which is just a simple function. And I've spelled abstract control wrong, let's just fix that quickly. So we're exporting a function called, not all caps, but notice that the function returns another function. The function that it returns is a validator function. So the function that gets returned from our validator will be the function that Angular invokes and passes a control to in order to validate it. So this is pretty similar to the other validator that we created in the last lesson, it will also return an object if the validation fails. We've used an array function here, and this function will receive a control and that will be of the type, abstract control. And this will be the control from the form that is being validated. So inside the validation function then, we just want to test whether the whole of the value of the text area is in upper case. And if so, we can return an object. So we've got a simple regular expression here that just checks whether or not the value is any upper case letter between A to Z, one or more times. And that constitutes the whole of the value from start to finish, and the regular expression should be global. So that should be all we need. We can use the test method of the regular expression to test it again the value of the control. And if we find that it is all in upper case, we can return this object with a not all caps key and a message that says, no shouting please. So this is much simpler than the validation directive that we created in the last lesson. So now, we'll want to use this in the feedback components. So first of all we'll need to import it there. The message control that we create in the ngOnInit, already has the required validator, so we should use the compose method to compose the existing validator with the new one that we've just created. As our validator contains a function which returns the actual validator, we should invoke the function that we imported in order to pass the actual validator function to Angular. So last of all, let's add a new error message for this in the template. That should be all we need to do. So let's go back to the browser now. And let's open up the feedback form, and let's add a shouty message. Okay, so it looks like the custom validator is working. So, in this lesson we saw how to create a custom validator function that we can use with our reactive forms. The validator function is much simpler without the additional directive stuff in there, and using the custom validator in the component couldn't be any easier. We use it almost as if it was just one of Angular's own built-in validators. The only trade off is that we can't use this validator in a template-driven form, not without wrapping it in a directive. Thanks for watching.