Skip to content

Commit 05fc7b4

Browse files
committed
completed handling validation errors
1 parent 43dc17d commit 05fc7b4

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

Spring Boot/codingevents/.idea/workspace.xml

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Spring Boot/codingevents/src/main/java/org/launchcode/codingevents/controllers/EventController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.launchcode.codingevents.data.EventData;
55
import org.launchcode.codingevents.models.Event;
66
import org.springframework.stereotype.Controller;
7+
import org.springframework.validation.Errors;
78
import org.springframework.web.bind.annotation.*;
89
import org.springframework.ui.Model;
910

@@ -28,7 +29,13 @@ public String displayCreateEventForm(Model model) {
2829
}
2930

3031
@PostMapping("create")
31-
public String processCreateEventForm(@ModelAttribute Event newEvent) {
32+
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent, Errors errors, Model model) {
33+
if (errors.hasErrors()) {
34+
model.addAttribute("title", "Create Event");
35+
model.addAttribute("errorMsg", "Bad data!");
36+
return "events/create";
37+
}
38+
3239
EventData.add(newEvent);
3340
return "redirect:/events";
3441
}

Spring Boot/codingevents/src/main/java/org/launchcode/codingevents/models/Event.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Event {
1818
@Size(max = 500, message = "Description too long!")
1919
private String description;
2020

21+
@NotBlank
2122
@Email(message = "Invalid email. Try again.")
2223
private String contactEmail;
2324

Spring Boot/codingevents/src/main/resources/templates/events/create.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
<header th:replace="fragments :: header"></header>
77

8+
<p th:text="${errorMsg}" style="color:red;"></p>
89

910
<form method="post">
1011
<div class="form-group">

0 commit comments

Comments
 (0)