Spring Boot Bootcamp – Workbooks and Challenges
)}

Challenge

Goal: validate the Item fields before submitting the form.

Launch the Starter Project

Screen Shot 2022-08-15 at 9.40.16 PM.png

Task 1

Add the validation dependency.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

The validation dependency provides the library of files needed to validate fields.

Task 2

Apply the following constraints to each field:

  • If the submitted category is blank, display: "Please choose a category"
  • If the submitted name is blank, display: "Name cannot be blank"
  • If the submitted price is less than 0, display: Price cannot be negative.
  • If the submitted discount is less than 0, display: Discount cannot be negative.
  • If the submitted date is not in the past, display: Date must be in the past.

Screen Shot 2022-06-02 at 9.05.32 PM.png

Task 3

Upon returning the form, the drop-down list loses its data.

Screen Shot 2022-06-05 at 5.14.11 PM.png

Since the data is fixed:

  • Remove the model.addAttribute("categories", categories) line from your handler method.
  • It makes more sense to write the values directly inside the HTML.
<option style="color:blue" value="">Choose Category</option> <option style="color:blue" value="Furniture">Furniture</option> <option style="color:blue" value="Office Supplies">Office Supplies</option> <option style="color:blue" value="Technology">Technology</option>

Output


Screen Shot 2022-05-25 at 8.06.56 PM.png

Task 4

Remove the CATEGORIES constant from Constants.java. It was only there for you to practice Thymeleaf loops, but its presence in the backend is not useful.

Task 5

The price cannot be less than the discount. Apply cross-field validation to the price and discount.

Screen Shot 2022-06-02 at 9.10.26 PM.png

Good Luck!

Feedback Summary
5.0
3 students
5

100%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.