Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that our form has some validation rules applied we can check the state of those inputs. In this video we'll look at different aspects of our form's state. We'll learn about dirty checking and how to tell if the user has touched the form.
-
0:00
Right now our form is validated by using the browser's
-
0:03
built in form validation methods.
-
0:05
In order to apply custom validation messages we'll need to disable the form's
-
0:10
validation and handle the validation ourselves when the form is submitted.
-
0:16
Let's go ahead and open up the entry-comment-form.component.html.
-
0:21
Let's add the now Validate attribute to the form element.
-
0:28
By adding the no validate attribute we're letting the browser know
-
0:32
that we'll be taking care of the validating of the form and
-
0:35
to suppress any validation messages when the form is submitted, now.
-
0:41
When we submit a form without any values in the name or comment,
-
0:45
we'll see the same empty rows being added to the comment list above.
-
0:53
To prevent these empty rows from being added, and invalid data being
-
0:56
sent to the backend server, We'll need to check the form state when it's submitted.
-
1:01
Open up the EntryCommentFormComponent.ts file.
-
1:05
In the submit method, we'll check if our form is valid.
-
1:10
If it's not valid, we won't allow the function to execute.
-
1:19
We'll add a simple check at the top of the un submit method body.
-
1:27
Let's see if that prevented the form from being submitted and
-
1:30
empty rows being added to the list of comments.
-
1:33
Okay, it looks good.
-
1:34
That solves a part of the problem.
-
1:36
Now, we need validation messages added to the form when the form is invalid.
-
1:41
The quickest way to add validation messages to our form
-
1:44
is by hard curtaining the messages in the template.
-
1:46
Let's open up the entry comment form component.html.
-
1:53
Beneath the name input add a
and- 1:59
give it the classes alert and
- 2:04
alert danger,
- 1:59
-
2:15
The alert and the alert danger classes aren't specific to angular.
-
2:20
These are only for styling the div if it's visible to the user.
-
2:24
Before we write our custom messages, we need to create a local variable on
-
2:28
the input element so that we can check the state as the user makes changes.
-
2:37
This local variable can be used anywhere in the view.
-
2:40
On the name input field, we'll add a local variable NameField
-
2:46
and assign ngModel to the variable.
-
2:54
With that, we can create our custom validation messages.
-
2:58
Inside the alert div, we'll add another div to indicate the field is required.
-
3:12
To show this field only when the field is left blank, we can bind the hidden
-
3:17
attribute of the div with the expression that evaluates to a truth value.
-
3:22
In our case, we'll check if the field has the required error.
-
3:36
Because we created the local variable name field,
-
3:40
we now have to access to the errors on the field.
-
3:44
We put the square brackets around the hidden attribute which means that this
-
3:49
will toggle the attribute on when the expression evaluates to true.
-
3:57
Save the file and take a look in the browser.
-
4:02
Okay, so a validation message shows up even though we haven't touched the form
-
4:08
and if we look at the browsers console, we can see an error has occurred.
-
4:14
When we added the hidden attribute on the div
-
4:16
we were preventing angular from evaluating the code.
-
4:20
And since our forms hasn't been touched we haven't triggered angular form validate's
-
4:25
yet, which means the arrays property is null,
-
4:29
resulting in the exception in the code.
-
4:32
To avoid this exception we need to completely hide
-
4:36
the validation messages from the form, until we're ready for them to be shown.
-
4:41
We can use the ngIf directive, which will add or
-
4:45
remove elements from the document, based on some condition.
-
4:48
In our case, we'll be setting an expression that retains true only
-
4:54
if the form is dirty, or has been touched, and the field has errors.
-
4:59
Let's at the ngIf directive like this.
-
5:03
In the ngIf expression, we'll add nameField.dirty.
-
5:12
Or nameField.touched.
-
5:22
And
-
5:23
nameField.errors.
-
5:32
Dirty, meaning if the name filed has changed.
-
5:36
Touched, if it's been touched by user.
-
5:39
And, if there's an errors.
-
5:43
Back in the browser, I'll submit the form without touching any of the fields.
-
5:47
The form doesn't get submitted but
-
5:49
our required validation method isn't available.
-
5:52
Let's see if I click in the Name field, but don't type anything,
-
5:56
ahen click on Submit.
-
5:59
When I clicked in the name field and
-
6:01
then focus left, I was clicking the submit button.
-
6:05
The state of the input changed from untouched to touched.
-
6:09
We can watch this happen in the browser.
-
6:11
Let's refresh the page.
-
6:15
And inspect the name label.
-
6:20
I don't want to accidentally trigger the touched event on the name field.
-
6:25
Once I find the field in div tools, I can watch the classes of the element change.
-
6:31
Right now, the field has the class of ng-untouched.
-
6:38
If I put my mouse in the field, nothing changes.
-
6:42
As soon as I click on the Submit button, I can see that
-
6:47
ngunsearched is removed and is immediately replaced with ngtouched.
-
6:54
Since I've submitted the form,
-
6:56
I also want to check the validations on this message if the form was submitted.
-
7:02
Inside of our expression, let's add another all case and
-
7:09
set it to comment form.submitted Now
-
7:14
whenever the forum is submitted we can
-
7:19
trigger our validation messages.
-
7:24
Let's go ahead and add our messages in.
-
7:37
Let's create a message for our minimum length.
-
7:44
Name must be a minimum
-
7:50
of three characters, Long.
-
8:14
On the text area let's use the same error messages as the name field above but
-
8:20
use the comment field local variable.
-
8:39
And let's change all references to name to comment.
-
8:49
We should also add the local variable to the comment text area.
-
9:05
Cool.
-
9:13
Now when we submit the form we can see why the form didn't fully process.
-
9:18
This is great.
-
9:19
We've got basic form validation happening.
-
9:22
Good job.
-
9:23
In the next video we'll look at styling the inputs based on their current state.
-
9:28
See you there.
You need to sign up for Treehouse in order to download course files.
Sign up