-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from LaunchCodeEducation/chapter15
Chapter 15: Model Validation
- Loading branch information
Showing
10 changed files
with
958 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: "Chapter 15: Model Validation" | ||
date: 2023-07-17T15:23:11-05:00 | ||
draft: false | ||
weight: 15 | ||
originalAuthor: Sally Steuterman # to be set by page creator | ||
originalAuthorGitHub: gildedgardenia # to be set by page creator | ||
reviewer: # to be set by the page reviewer | ||
reviewerGitHub: # to be set by the page reviewer | ||
lastEditor: # update any time edits are made after review | ||
lastEditorGitHub: # update any time edits are made after review | ||
lastMod: # UPDATE ANY TIME CHANGES ARE MADE | ||
--- | ||
|
||
## Learning Objectives | ||
|
||
Upon completing all the content in this chapter, you should be able to do the following: | ||
|
||
1. Validate form submission data using annotations and model binding | ||
|
||
## Key Terminology | ||
|
||
Here is a list of key terms for this chapter listed by the page the terms first appear on. | ||
|
||
### Server-Side Validation | ||
|
||
1. client-side validation | ||
1. server-side validation | ||
|
||
### Thymeleaf Form Tools | ||
|
||
1. no-arg constructor | ||
|
||
## Chapter Content | ||
|
||
{{% children %}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: "Exercises: Model Validation" | ||
date: 2023-07-17T15:23:11-05:00 | ||
draft: false | ||
weight: 2 | ||
originalAuthor: Sally Steuterman # to be set by page creator | ||
originalAuthorGitHub: gildedgardenia # to be set by page creator | ||
reviewer: # to be set by the page reviewer | ||
reviewerGitHub: # to be set by the page reviewer | ||
lastEditor: # update any time edits are made after review | ||
lastEditorGitHub: # update any time edits are made after review | ||
lastMod: # UPDATE ANY TIME CHANGES ARE MADE | ||
--- | ||
|
||
Let’s practice adding more fields onto our event objects and | ||
validating them. Create a new branch from your own `display-errors` branch. Here's the `display-errors` branch on | ||
[CodingEventsJava](https://github.com/LaunchCodeEducation/CodingEventsJava/tree/display-errors) if you need to get up to speed. | ||
|
||
Below, we describe some new fields for you to add to the `Event` class. | ||
For each field, consider the following factors: | ||
|
||
1. What will you call your field? | ||
1. Will you need accessors for this field? | ||
1. What type of input should be added to capture the field's information from the user? | ||
1. Refer to the documentation page to find an appropriate annotation to fit the constraints. | ||
1. What should the error message convey to the user? | ||
1. What, if anything, will you need to update on the controller to account for the new field? | ||
|
||
Event information to add: | ||
|
||
1. Add a field to collect information about where the event will take place. This field should not be | ||
null or blank. | ||
|
||
{{% expand "Check your solution" %}} | ||
|
||
```java | ||
@NotBlank(message="Location cannot be left blank.") | ||
private String location; | ||
``` | ||
|
||
{{% /expand %}} | ||
|
||
1. Add a field to collect information about whether an attendee must register for the event or not. For | ||
the purposes of validation practice, make this field only able to be marked as true. | ||
|
||
1. Add a field to collect information about the number of attendees for the event. Valid values for this | ||
field should be any number over zero. | ||
|
||
{{% expand "Check your solution" %}} | ||
|
||
```java | ||
@Positive(message="Number of attendees must be one or more.") | ||
private int numberOfAttendees; | ||
``` | ||
|
||
{{% /expand %}} | ||
|
||
1. Browse the validation annotations to find one to use on another new field of your choosing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: "Next Steps" | ||
date: 2023-07-17T15:23:11-05:00 | ||
draft: false | ||
weight: 4 | ||
originalAuthor: Sally Steuterman # to be set by page creator | ||
originalAuthorGitHub: gildedgardenia # to be set by page creator | ||
reviewer: # to be set by the page reviewer | ||
reviewerGitHub: # to be set by the page reviewer | ||
lastEditor: # update any time edits are made after review | ||
lastEditorGitHub: # update any time edits are made after review | ||
lastMod: # UPDATE ANY TIME CHANGES ARE MADE | ||
--- | ||
|
||
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. | ||
|
||
1. [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation) | ||
1. [Validating Form Input](https://spring.io/guides/gs/validating-form-input/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: "Reading" | ||
date: 2023-07-17T15:23:11-05:00 | ||
draft: false | ||
weight: 1 | ||
originalAuthor: Sally Steuterman # to be set by page creator | ||
originalAuthorGitHub: gildedgardenia # to be set by page creator | ||
reviewer: # to be set by the page reviewer | ||
reviewerGitHub: # to be set by the page reviewer | ||
lastEditor: # update any time edits are made after review | ||
lastEditorGitHub: # update any time edits are made after review | ||
lastMod: # UPDATE ANY TIME CHANGES ARE MADE | ||
--- | ||
|
||
## Reading Content Links | ||
|
||
{{% children %}} |
45 changes: 45 additions & 0 deletions
45
content/spring-model-validation/reading/server-side-validation/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
title: "Server-Side Validation" | ||
date: 2023-07-17T15:23:11-05:00 | ||
draft: false | ||
weight: 1 | ||
originalAuthor: Sally Steuterman # to be set by page creator | ||
originalAuthorGitHub: gildedgardenia # to be set by page creator | ||
reviewer: # to be set by the page reviewer | ||
reviewerGitHub: # to be set by the page reviewer | ||
lastEditor: # update any time edits are made after review | ||
lastEditorGitHub: # update any time edits are made after review | ||
lastMod: # UPDATE ANY TIME CHANGES ARE MADE | ||
--- | ||
|
||
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. | ||
|
||
{{% notice blue "Example" "rocket" %}} | ||
|
||
Consider a user registration form on a web site. Effective validation rules might require that: | ||
|
||
1. The username is between 3 and 12 characters long, and | ||
1. The password is at least 6 characters long. | ||
|
||
{{% /notice %}} | ||
|
||
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**. | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Check Your Understanding | ||
|
||
{{% notice green "Question" "rocket" %}} | ||
|
||
The best practice for validating data in a web app is to: | ||
|
||
1. Use client-side validation | ||
1. Use server-side validation | ||
1. Use both client-side and server-side validation | ||
1. Don't validate incoming data | ||
|
||
{{% /notice %}} | ||
|
||
<!-- c, Use both client-side and server-side validation --> |
Oops, something went wrong.