Skip to content

Commit 0b9f55c

Browse files
Merge pull request #27 from LaunchCodeEducation/chapter15
Chapter 15: Model Validation
2 parents 90e60b7 + 83ef9d3 commit 0b9f55c

File tree

10 files changed

+958
-1
lines changed

10 files changed

+958
-1
lines changed

content/enums/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Chapter 16: Enums"
33
date: 2021-10-01T09:28:27-05:00
44
draft: false
5-
weight: 14
5+
weight: 16
66
originalAuthor: John Woolbright # to be set by page creator
77
originalAuthorGitHub: jwoolbright23 # to be set by page creator
88
reviewer: Sally Steuterman # to be set by the page reviewer
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: "Chapter 15: Model Validation"
3+
date: 2023-07-17T15:23:11-05:00
4+
draft: false
5+
weight: 15
6+
originalAuthor: Sally Steuterman # to be set by page creator
7+
originalAuthorGitHub: gildedgardenia # to be set by page creator
8+
reviewer: # to be set by the page reviewer
9+
reviewerGitHub: # to be set by the page reviewer
10+
lastEditor: # update any time edits are made after review
11+
lastEditorGitHub: # update any time edits are made after review
12+
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
13+
---
14+
15+
## Learning Objectives
16+
17+
Upon completing all the content in this chapter, you should be able to do the following:
18+
19+
1. Validate form submission data using annotations and model binding
20+
21+
## Key Terminology
22+
23+
Here is a list of key terms for this chapter listed by the page the terms first appear on.
24+
25+
### Server-Side Validation
26+
27+
1. client-side validation
28+
1. server-side validation
29+
30+
### Thymeleaf Form Tools
31+
32+
1. no-arg constructor
33+
34+
## Chapter Content
35+
36+
{{% children %}}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Exercises: Model Validation"
3+
date: 2023-07-17T15:23:11-05:00
4+
draft: false
5+
weight: 2
6+
originalAuthor: Sally Steuterman # to be set by page creator
7+
originalAuthorGitHub: gildedgardenia # to be set by page creator
8+
reviewer: # to be set by the page reviewer
9+
reviewerGitHub: # to be set by the page reviewer
10+
lastEditor: # update any time edits are made after review
11+
lastEditorGitHub: # update any time edits are made after review
12+
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
13+
---
14+
15+
Let’s practice adding more fields onto our event objects and
16+
validating them. Create a new branch from your own `display-errors` branch. Here's the `display-errors` branch on
17+
[CodingEventsJava](https://github.com/LaunchCodeEducation/CodingEventsJava/tree/display-errors) if you need to get up to speed.
18+
19+
Below, we describe some new fields for you to add to the `Event` class.
20+
For each field, consider the following factors:
21+
22+
1. What will you call your field?
23+
1. Will you need accessors for this field?
24+
1. What type of input should be added to capture the field's information from the user?
25+
1. Refer to the documentation page to find an appropriate annotation to fit the constraints.
26+
1. What should the error message convey to the user?
27+
1. What, if anything, will you need to update on the controller to account for the new field?
28+
29+
Event information to add:
30+
31+
1. Add a field to collect information about where the event will take place. This field should not be
32+
null or blank.
33+
34+
{{% expand "Check your solution" %}}
35+
36+
```java
37+
@NotBlank(message="Location cannot be left blank.")
38+
private String location;
39+
```
40+
41+
{{% /expand %}}
42+
43+
1. Add a field to collect information about whether an attendee must register for the event or not. For
44+
the purposes of validation practice, make this field only able to be marked as true.
45+
46+
1. Add a field to collect information about the number of attendees for the event. Valid values for this
47+
field should be any number over zero.
48+
49+
{{% expand "Check your solution" %}}
50+
51+
```java
52+
@Positive(message="Number of attendees must be one or more.")
53+
private int numberOfAttendees;
54+
```
55+
56+
{{% /expand %}}
57+
58+
1. Browse the validation annotations to find one to use on another new field of your choosing.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: "Next Steps"
3+
date: 2023-07-17T15:23:11-05:00
4+
draft: false
5+
weight: 4
6+
originalAuthor: Sally Steuterman # to be set by page creator
7+
originalAuthorGitHub: gildedgardenia # to be set by page creator
8+
reviewer: # to be set by the page reviewer
9+
reviewerGitHub: # to be set by the page reviewer
10+
lastEditor: # update any time edits are made after review
11+
lastEditorGitHub: # update any time edits are made after review
12+
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
13+
---
14+
15+
Before diving into the next chapter, here are some of our favorite resources on model validation in Spring Boot in case you want to review them.
16+
17+
1. [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
18+
1. [Validating Form Input](https://spring.io/guides/gs/validating-form-input/)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: "Reading"
3+
date: 2023-07-17T15:23:11-05:00
4+
draft: false
5+
weight: 1
6+
originalAuthor: Sally Steuterman # to be set by page creator
7+
originalAuthorGitHub: gildedgardenia # to be set by page creator
8+
reviewer: # to be set by the page reviewer
9+
reviewerGitHub: # to be set by the page reviewer
10+
lastEditor: # update any time edits are made after review
11+
lastEditorGitHub: # update any time edits are made after review
12+
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
13+
---
14+
15+
## Reading Content Links
16+
17+
{{% children %}}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: "Server-Side Validation"
3+
date: 2023-07-17T15:23:11-05:00
4+
draft: false
5+
weight: 1
6+
originalAuthor: Sally Steuterman # to be set by page creator
7+
originalAuthorGitHub: gildedgardenia # to be set by page creator
8+
reviewer: # to be set by the page reviewer
9+
reviewerGitHub: # to be set by the page reviewer
10+
lastEditor: # update any time edits are made after review
11+
lastEditorGitHub: # update any time edits are made after review
12+
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
13+
---
14+
15+
Web applications work under the client-server model. We have been focusing on the server portion, using Spring Boot and Java to create server-side application code. A critical component of any well-made web application is **validation**, which is the process of checking that data conforms to certain criteria. Validation ensures that the application only stores meaningful data.
16+
17+
{{% notice blue "Example" "rocket" %}}
18+
19+
Consider a user registration form on a web site. Effective validation rules might require that:
20+
21+
1. The username is between 3 and 12 characters long, and
22+
1. The password is at least 6 characters long.
23+
24+
{{% /notice %}}
25+
26+
Web applications should validate *all* data submitted by users. This ensures that data remains well-structured and unexpected errors don't occur. Validation that occurs in the browser---using JavaScript or HTML attributes---is **client-side validation**. Validation that occurs on the web server is **server-side validation**.
27+
28+
Even if client-side validation is done, it is still critical to validate data on the server. This is because client-side validation can often be bypassed by a savvy user. For example, such a user might modify HTML using a browser's developer tools, or disable JavaScript.
29+
30+
Server-side validation involves both the model and controller. The model is responsible for *defining* validation rules, while the controller is responsible for *checking* validation rules when data is submitted to the server.
31+
32+
## Check Your Understanding
33+
34+
{{% notice green "Question" "rocket" %}}
35+
36+
The best practice for validating data in a web app is to:
37+
38+
1. Use client-side validation
39+
1. Use server-side validation
40+
1. Use both client-side and server-side validation
41+
1. Don't validate incoming data
42+
43+
{{% /notice %}}
44+
45+
<!-- c, Use both client-side and server-side validation -->

0 commit comments

Comments
 (0)