Skip to content

Commit bf2a238

Browse files
committed
Model validation exercise complete
1 parent 93fe19d commit bf2a238

6 files changed

Lines changed: 90 additions & 26 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public String displayAllEvents(Model model) {
2828
@GetMapping("create")
2929
public String displayCreateEventForm(Model model) {
3030
model.addAttribute("title", "Create Event");
31-
model.addAttribute(new Event());
31+
model.addAttribute("event", new Event());
3232
return "events/create";
3333
}
3434

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.launchcode.codingevents.models;
22

3-
import jakarta.validation.constraints.Email;
4-
import jakarta.validation.constraints.NotBlank;
5-
import jakarta.validation.constraints.Size;
3+
import jakarta.validation.constraints.*;
64

75
import java.util.Objects;
86

@@ -24,16 +22,36 @@ public class Event {
2422
@NotBlank(message = "Email is required")
2523
@Email(message = "Invalid email. Try again.")
2624
private String contactEmail;
25+
@AssertTrue
26+
private boolean mustRegister;
27+
@Positive(message = "Number must be one or more.")
28+
private int numberOfAttendees;
2729

28-
public Event(String name, String description, String contactEmail) {
30+
@NotBlank(message = "Cannot be blank!!!! C'mon, do better!!!!")
31+
@Size(max = 100, message = "Description too long. 100 char max. You trying to write a novel or something?")
32+
private String location;
33+
@Positive(message = "Be positive, man!")
34+
private int duration;
35+
36+
37+
38+
public Event(String name, String description, String contactEmail, String location, boolean mustRegister, int numberOfAttendees, int duration) {
39+
this();
2940
this.name = name;
3041
this.description = description;
3142
this.contactEmail = contactEmail;
43+
this.location = location;
44+
this.mustRegister = true;
45+
this.numberOfAttendees = numberOfAttendees;
46+
this.duration = duration;
47+
48+
}
49+
50+
public Event() {
3251
this.id = nextId;
3352
nextId++;
3453
}
3554

36-
public Event() {}
3755
public String getName() {
3856
return name;
3957
}
@@ -63,6 +81,38 @@ public int getId() {
6381
return id;
6482
}
6583

84+
public boolean isMustRegister() {
85+
return mustRegister;
86+
}
87+
88+
public void setMustRegister(boolean mustRegister) {
89+
this.mustRegister = mustRegister;
90+
}
91+
92+
public int getNumberOfAttendees() {
93+
return numberOfAttendees;
94+
}
95+
96+
public void setNumberOfAttendees(int numberOfAttendees) {
97+
this.numberOfAttendees = numberOfAttendees;
98+
}
99+
100+
public String getLocation() {
101+
return location;
102+
}
103+
104+
public void setLocation(String location) {
105+
this.location = location;
106+
}
107+
108+
public int getDuration() {
109+
return duration;
110+
}
111+
112+
public void setDuration(int duration) {
113+
this.duration = duration;
114+
}
115+
66116
@Override
67117
public String toString() {
68118
return name;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
body {
22
font-size: 18px;
3+
}
4+
.error {
5+
color: red;
36
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,46 @@
1010
<form method="post">
1111
<div class="form-group">
1212
<label>Name
13-
<input type="text" name="name" class="form-control">
13+
<input type="text" th:field="${event.name}" class="form-control">
1414
</label>
1515
</div>
16+
<p class="error" th:errors="${event.name}"></p>
1617
<div class="form-group">
1718
<label>Description
18-
<input type="text" name="description" class="form-control">
19+
<input type="text" th:field="${event.description}" class="form-control">
1920
</label>
2021
</div>
22+
<p class="error" th:errors="${event.description}"></p>
2123
<div class="form-group">
2224
<label>Contact Email
23-
<input type="text" name="contactEmail" class="form-control">
25+
<input type="text" th:field="${event.contactEmail}" class="form-control">
2426
</label>
2527
</div>
28+
<p class="error" th:errors="${event.contactEmail}"></p>
29+
<div class="form-group">
30+
<label>Location
31+
<input type="text" th:field="${event.location}" class="form-control">
32+
</label>
33+
</div>
34+
<p class="error" th:errors="${event.location}"></p>
35+
<div class="form-group">
36+
<label>Registration
37+
<input type="text" th:field="${event.mustRegister}" class="form-control">
38+
</label>
39+
</div>
40+
<p class="error" th:errors="${event.mustRegister}"></p>
41+
<div class="form-group">
42+
<label>Number of Attendees
43+
<input type="text" th:field="${event.numberOfAttendees}" class="form-control">
44+
</label>
45+
</div>
46+
<p class="error" th:errors="${event.numberOfAttendees}"></p>
47+
<div class="form-group">
48+
<label>Duration
49+
<input type="text" th:field="${event.duration}" class="form-control">
50+
</label>
51+
</div>
52+
<p class="error" th:errors="${event.duration}"></p>
2653
<div class="form-group">
2754
<input type="submit" value="Create" class="btn btn-success">
2855
</div>

src/main/resources/templates/fragments.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head th:fragment="head">
44
<meta charset="UTF-8"/>
55
<title>Coding Events</title>
6+
<link rel="stylesheet" th:href="@{/styles.css}">
67
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
78
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
89
</head>

0 commit comments

Comments
 (0)