Skip to content

Commit 5451588

Browse files
Using Thymeleaf to display error messages
1 parent 93fe19d commit 5451588

5 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
3737
Errors errors, Model model) {
3838
if(errors.hasErrors()) {
3939
model.addAttribute("title", "Create Event");
40-
model.addAttribute("errorMsg", "Bad data!");
4140
return "events/create";
4241
}
4342

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ public class Event {
2626
private String contactEmail;
2727

2828
public Event(String name, String description, String contactEmail) {
29+
this();
2930
this.name = name;
3031
this.description = description;
3132
this.contactEmail = contactEmail;
33+
}
34+
35+
public Event() {
3236
this.id = nextId;
3337
nextId++;
3438
}
35-
36-
public Event() {}
3739
public String getName() {
3840
return name;
3941
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
body {
22
font-size: 18px;
3+
}
4+
5+
.error {
6+
color: red;
37
}

src/main/resources/templates/events/create.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
<form method="post">
1111
<div class="form-group">
1212
<label>Name
13-
<input type="text" name="name" class="form-control">
13+
<input th:field="${event.name}" class="form-control">
1414
</label>
15+
<p class="error" th:errors="${event.name}"></p>
1516
</div>
1617
<div class="form-group">
1718
<label>Description
18-
<input type="text" name="description" class="form-control">
19+
<input th:field="${event.description}" class="form-control">
1920
</label>
21+
<p class="error" th:errors="${event.description}"></p>
2022
</div>
2123
<div class="form-group">
2224
<label>Contact Email
23-
<input type="text" name="contactEmail" class="form-control">
25+
<input th:field="${event.contactEmail}" class="form-control">
2426
</label>
27+
<p class="error" th:errors="${event.contactEmail}"></p>
2528
</div>
2629
<div class="form-group">
2730
<input type="submit" value="Create" class="btn btn-success">

src/main/resources/templates/fragments.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Coding Events</title>
66
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
77
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
8+
<link rel="stylesheet" th:href="@{/styles.css}">
89
</head>
910
<body>
1011

0 commit comments

Comments
 (0)