Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation #31

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hmm
  • Loading branch information
sara-bohannon committed Dec 15, 2024
commit 7d9ff48cf984482072fda816ff99f5a4e8b01aa4
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.launchcode.codingevents.controllers;

import jakarta.validation.Valid;
import org.launchcode.codingevents.data.EventData;
import org.launchcode.codingevents.models.Event;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
Expand All @@ -24,11 +26,19 @@ public String displayAllEvents(Model model) {
@GetMapping("create")
public String displayCreateEventForm(Model model) {
model.addAttribute("title", "Create Event");
model.addAttribute(new Event());
return "events/create";
}

@PostMapping("create")
public String processCreateEventForm(@ModelAttribute Event newEvent) {
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
Errors errors, Model model) {
if(errors.hasErrors()) {
model.addAttribute("title", "Create Event");
return "events/create";
}


EventData.add(newEvent);
return "redirect:/events";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
package org.launchcode.codingevents.models;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.Objects;

public class Event {

private int id;
private static int nextId = 1;

@NotBlank(message = "Name is required.")
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters." )
private String name;

@Size(max = 500, message = "Description too long!")
private String description;

public Event(String name, String description) {
@NotBlank(message = "Email is required")
@Email(message = "Invalid email, Try again.")
private String contactEmail;

public Event(String name, String description, String contactEmail) {
this.name = name;
this.description = description;
this.contactEmail = contactEmail;
this.id = nextId;
nextId++;
}

public Event() {}

public String getName() {
return name;
}
Expand All @@ -33,6 +48,14 @@ public void setDescription(String description) {
this.description = description;
}

public @Email String getContactEmail() {
return contactEmail;
}

public void setContactEmail(@Email String contactEmail) {
this.contactEmail = contactEmail;
}

public int getId() {
return id;
}
Expand Down
6 changes: 4 additions & 2 deletions codingevents/src/main/resources/static/styles.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
body {
font-size: 18px;

font-size: 14px;
}
.error {
color: red;
}
14 changes: 11 additions & 3 deletions codingevents/src/main/resources/templates/events/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
<form method="post">
<div class="form-group">
<label>Name
<input type="text" name="name" class="form-control">
</label>
<input th:field="${event.name}" class="form-control">
</label>
<p class="error" th:errors="${event.name}"></p>
</div>
<div class="form-group">
<label>Description
<input type="text" name="description" class="form-control">
<input th:field="${event.description}" class="form-control">
<p class="error" th:errors="${event.description}"></p>
</label>
</div>
<div class="form-group">
<label>Contact Email
<input th:field="${event.contactEmail}" class="form-control">
</label>
<p class="error" th:errors="${event.contactEmail}"></p>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-success">
Expand Down
2 changes: 2 additions & 0 deletions codingevents/src/main/resources/templates/events/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Contact Email</th>
</tr>
</thead>
<tr th:each="event : ${events}">
<td th:text="${event.id}"></td>
<td th:text="${event.name}"></td>
<td th:text="${event.description}"></td>
<td th:text="${event.contactEmail}"></td>
</tr>
</table>

Expand Down
1 change: 1 addition & 0 deletions codingevents/src/main/resources/templates/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>Coding Events</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" th:href="@{/styles.css}">
</head>
<body>

Expand Down