File tree Expand file tree Collapse file tree
java/org/launchcode/codingevents
resources/templates/events Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package org .launchcode .codingevents .controllers ;
22
3+ import jakarta .validation .Valid ;
34import org .launchcode .codingevents .data .EventData ;
45import org .launchcode .codingevents .models .Event ;
56import org .springframework .stereotype .Controller ;
7+ import org .springframework .validation .Errors ;
68import org .springframework .web .bind .annotation .*;
79import org .springframework .ui .Model ;
810
@@ -30,7 +32,14 @@ public String displayCreateEventForm(Model model) {
3032 }
3133
3234 @ PostMapping ("/create" )
33- public String processCreateEventForm (@ ModelAttribute Event newEvent ) {
35+ public String processCreateEventForm (@ ModelAttribute @ Valid Event newEvent ,
36+ Errors errors , Model model ) {
37+ if (errors .hasErrors ()){
38+ model .addAttribute ("title" , "Create Event" );
39+ model .addAttribute ("errorMsg" , "Bad Data" );
40+ return "events/create" ;
41+
42+ }
3443 EventData .add (newEvent );
3544 return "redirect:/events" ;
3645 }
Original file line number Diff line number Diff line change 11package org .launchcode .codingevents .models ;
22
3+ import jakarta .validation .constraints .Email ;
4+ import jakarta .validation .constraints .NotBlank ;
5+ import jakarta .validation .constraints .Size ;
6+
37import java .util .Objects ;
48
59public class Event {
610
711 private int id ;
812 private static int nextId = 1 ;
13+
14+ @ Size (min =3 , max =50 , message = "Name must be between 3 and 50 characters" )
15+ @ NotBlank
916 private String name ;
17+
18+ @ Size (max =500 , message = "Description too long." )
1019 private String description ;
1120
12- public Event (String name , String description ) {
21+ @ NotBlank
22+ @ Email (message = "Invalid Email. Please try again." )
23+ private String contactEmail ;
24+
25+ public Event (String name , String description , String contactEmail ) {
1326 this .name = name ;
1427 this .description = description ;
28+ this .contactEmail = contactEmail ;
1529 this .id = nextId ;
1630 nextId ++;
1731 }
@@ -32,6 +46,14 @@ public void setDescription(String description) {
3246 this .description = description ;
3347 }
3448
49+ public String getContactEmail () {
50+ return contactEmail ;
51+ }
52+
53+ public void setContactEmail (String contactEmail ) {
54+ this .contactEmail = contactEmail ;
55+ }
56+
3557 public int getId () {
3658 return id ;
3759 }
Original file line number Diff line number Diff line change 55
66< header th:replace ="fragments :: header "> </ header >
77
8-
8+ < p th:text =" ${errorMsg} " style =" color:red; " > </ p >
99< form method ="post ">
1010 < div class ="form-group ">
1111 < label > Name
1717 < input type ="text " name ="description " class ="form-control ">
1818 </ label >
1919 </ div >
20+ < div class ="form-group ">
21+ < label > Contact Email
22+ < input type ="text " name ="contactEmail " class ="form-control ">
23+ </ label >
24+ </ div >
2025 < div class ="form-group ">
2126 < input type ="submit " value ="Create " class ="btn btn-success ">
2227 </ div >
Original file line number Diff line number Diff line change 1313 < th > ID</ th >
1414 < th > Name</ th >
1515 < th > Description</ th >
16+ < th > Contact Email</ th >
1617 </ tr >
1718 </ thead >
1819 < tr th:each ="event : ${events} ">
1920 < td th:text ="${event.id} "> </ td >
2021 < td th:text ="${event.name} "> </ td >
2122 < td th:text ="${event.description} "> </ td >
23+ < td th:text ="${event.contactEmail} "> </ td >
2224 </ tr >
2325</ table >
2426
You can’t perform that action at this time.
0 commit comments