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
After adding some annotation constraints to our Category class, we cover the code necessary for triggering validation and capturing errors.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s4v3
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
https://github.com/treehouse/giflib-hibernate
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone git@github.com:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the git checkout
command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Regular Expressions Reference
-
0:00
The process of adding form validation in Spring is threefold.
-
0:04
First, we'll annotate the entity class' fields with JPA validation annotations,
-
0:10
which come from the javax.validation.constraints package.
-
0:14
Then, we'll capture validation in what's called
-
0:16
a BindingResult in the controller final.
-
0:20
If there are validation errors, we'll redirect back to the form and
-
0:23
display those validation errors.
-
0:26
Let's get started by adding those validation annotations and by redirecting
-
0:30
back to the form without adding the category when errors are encountered.
-
0:36
The first step I mentioned was adding validation annotations and
-
0:39
we'll do that in the entity classes.
-
0:41
Let's head to the category class.
-
0:43
So under the model package, I will open the category class.
-
0:47
Here, I'll demonstrate just a few of the annotations we can use.
-
0:51
Let's start with the name field.
-
0:54
Let's make a decision that the value has to be present and
-
0:57
that its length must be between, say, three and twelve characters.
-
1:01
You can change this, if you'd like.
-
1:03
To do this, we'll add the NotNull annotation,
-
1:06
which will ensure that it's not blank, as well as the Size annotation.
-
1:12
And here, we can specify a minimum and maximum value.
-
1:18
So I'll say min = 3 and max = 12.
-
1:22
Next, let's add validation annotations for the colorCode field.
-
1:26
So I'll do that right here.
-
1:29
This value should be a valid hexadecimal color code as used in CSS, so
-
1:33
let's mark it as NotNull, so we make sure it's provided, and to ensure
-
1:40
that the value is a valid hexadecimal value, let's use the Pattern annotation.
-
1:46
Now, I'm going to use a regular expression here on which we could have an entire
-
1:50
course.
-
1:51
Check the teacher's notes for more regular expression info, and just note for
-
1:56
our purposes here, that regular expressions can be used to
-
2:00
intelligently detect patterns in strings.
-
2:04
So, I will start with the regular expression element in the annotation here.
-
2:10
And as far as the pattern I'm checking for, I'll enclose that in quotes.
-
2:14
I'd like it to start with a hash sign cuz a hex code in CSS starts with a hash sign.
-
2:19
And have each subsequent character come from the character class of either zero
-
2:24
through nine, lowercase a through f, uppercase A through
-
2:30
F, and I'd like that to be repeated six times.
-
2:36
And it looks like my right bracket snuck in there, as well.
-
2:40
There was IntelliJ doing all the work for me.
-
2:44
Okay.
-
2:44
That's all I'd like to validate in this entity.
-
2:47
Next, we'll go to the category controller to trigger validation and
-
2:51
capture the results.
-
2:53
Here, we'll focus on the add category method, which is down a bit, there we go.
-
3:00
What I'd like to do here is first trigger validation according to the constraints we
-
3:03
just added to the category entity.
-
3:05
And it turns out, this is really simple.
-
3:08
We add a valid annotation to the category parameter.
-
3:12
So this category parameter, before, we add the Valid annotation.
-
3:17
And just like that, it will trigger validation,
-
3:19
according to the constraints that we put on the category class.
-
3:24
Next, we'll need a way to capture errors, and in Spring,
-
3:27
this is most easily accomplished by including a binding result parameter, so
-
3:31
I’ll add that after the category parameter, BindingResult,
-
3:35
and I’ll just call it, result.
-
3:38
Now what we need to do is actually take alternate action if errors are found,
-
3:42
such as redirecting back to the form.
-
3:45
To detect errors, we can use an if statement.
-
3:48
I'll do that before our categories save.
-
3:50
So we can say if the result has errors,
-
3:55
what we can do here is redirect back to the form.
-
4:03
And to do that, we'll return a redirect back to the you URI that's used
-
4:09
to render the form, remember, this is not the name of the Thymeleaf view, but rather
-
4:15
the URI that will need to be requested in order for the form to be rendered.
-
4:21
Okay.
-
4:22
We're ready to test this.
-
4:23
Let's reboot and try to add a category with invalid data.
-
4:27
So I’m going to stop my current instance of my app, and
-
4:32
then I can reboot that thing, and
-
4:36
we'll wait for the app to both compile and fully boot.
-
4:41
There we go, application started, I'll switch back to Chrome.
-
4:45
Let me first try to add a category without supplying any data whatsoever.
-
4:50
So I'll add a category, and there we are, right back at the form, it may look like
-
4:55
nothing happened, but I certainly clicked add and got right back to the form.
-
5:00
Let me add a two character category name, and a color.
-
5:04
Remember, we said that the category name had to be between three and
-
5:08
twelve characters in length.
-
5:10
So I'll say Sp, and then I'll choose Blue, and click Add.
-
5:16
And once again, we're right back at the form, and
-
5:18
finally, let's make sure that we can successfully add a category if we do,
-
5:24
indeed, provide valid data for all fields.
-
5:27
So, I will type Spring here.
-
5:30
I'll choose Blue, why not, and click Add.
-
5:35
Great, there it is.
-
5:37
Now we still have more work to do, but
-
5:38
you're well on your way to fully implemented form validation.
-
5:42
So we've taken the critical step of introducing server side validation
-
5:46
into the flow of our application data, but we left out another critical piece.
-
5:51
When our form was re-displayed, we offered no indication that there was an error.
-
5:56
This could certainly leave users confused as to what just happened.
-
5:59
So next, we'll add form validation messages that tell users
-
6:03
exactly what went wrong when something does go wrong.
You need to sign up for Treehouse in order to download course files.
Sign up