|
| 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. |
0 commit comments