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
@@ -58,7 +60,15 @@ public String displayCreateEventForm(Model model) {
5860
5961 //Model binding. It creats an object newEvent from Model Event
6062 @ PostMapping ("create" )
61- public String processCreateEventForm (@ ModelAttribute Event newEvent ) {
63+ public String processCreateEventForm (@ ModelAttribute @ Valid Event newEvent ,
64+ Errors errors , Model model ) {
65+
66+ if (errors .hasErrors ()){
67+ model .addAttribute ("title" , "Create Event" );
68+ model .addAttribute ("errorMsg" , "Bad data" );
69+ return "events/create" ;
70+ }
71+
6272 EventData .add (newEvent );
6373 return "redirect:/events" ;
6474 }
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 ;
913
14+ @ NotBlank
15+ @ Size (min = 3 , max = 50 , message = "Name must be between 3 and 50 characters" )
1016 private String name ;
17+
18+ @ Size (max = 500 , message = "Description too long!" )
1119 private String description ;
1220
13- public Event (String name , String description ) {
21+ @ NotBlank
22+ @ Email (message = "Invalid email. Try again." )
23+ private String contactEmail ;
24+
25+ public Event (String name , String description , String contactEmail ) {
1426 this .name = name ;
1527 this .description = description ;
28+ this .contactEmail = contactEmail ;
1629 this .id = nextId ;
1730 nextId ++;
1831 }
@@ -33,6 +46,14 @@ public void setDescription(String description) {
3346 this .description = description ;
3447 }
3548
49+ public @ Email String getContactEmail () {
50+ return contactEmail ;
51+ }
52+
53+ public void setContactEmail (@ Email String contactEmail ) {
54+ this .contactEmail = contactEmail ;
55+ }
56+
3657 public int getId () {
3758 return id ;
3859 }
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ <h1>Create Event</h1>
77
88< nav th:replace ="fragments :: navigation "> </ nav >
99
10+ < p th:text ="${errorMsg} " style ="color:red; "> </ p >
11+
1012< form method ="post ">
1113 < div class ="form-group ">
1214 < label > Name
@@ -20,6 +22,12 @@ <h1>Create Event</h1>
2022 </ label >
2123 </ div >
2224
25+ < div class ="form-group ">
26+ < label > Contact Email
27+ < input type ="text " name ="contactEmail " class ="form-control ">
28+ </ label >
29+ </ div >
30+
2331 < div class ="form-group ">
2432 < input type ="submit " value ="Create Event " class ="btn btn-success ">
2533 </ div >
Original file line number Diff line number Diff line change @@ -29,12 +29,14 @@ <h1>All Events</h1>
2929 < th > Name</ th >
3030 < th > Description</ th >
3131 < th > Edit</ th >
32+ < th > Contact Email</ th >
3233 </ tr >
3334 </ thead >
3435 < tr th:each ="event : ${events} ">
3536 < td th:text ="${event.id} "> </ td >
3637 < td th:text ="${event.name} "> </ td >
3738 < td th:text ="${event.description} "> </ td >
39+ < td th:text ="${event.contactEmail} "> </ td >
3840 < td > < a th:href ="@{/events/edit/{id}(id=${event.id})} "> Edit</ a > </ td >
3941 </ tr >
4042</ table >
You can’t perform that action at this time.
0 commit comments