Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
s7_spruge_k
calendar
Commits
f0aee202
Commit
f0aee202
authored
Jun 14, 2019
by
s7_spruge_k
Browse files
Woring Event validations
parent
dc84d1ec
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
10 deletions
+70
-10
src/main/java/com/example/calendar/controller/EventController.java
...java/com/example/calendar/controller/EventController.java
+5
-2
src/main/java/com/example/calendar/models/Event.java
src/main/java/com/example/calendar/models/Event.java
+0
-3
src/main/java/com/example/calendar/validator/EventValidator.java
...n/java/com/example/calendar/validator/EventValidator.java
+57
-0
src/main/resources/templates/addNewEvent.html
src/main/resources/templates/addNewEvent.html
+5
-4
src/main/resources/validation.properties
src/main/resources/validation.properties
+3
-1
No files found.
src/main/java/com/example/calendar/controller/EventController.java
View file @
f0aee202
...
...
@@ -18,6 +18,8 @@ import com.example.calendar.models.User;
import
com.example.calendar.repo.CalendarRepo
;
import
com.example.calendar.repo.EventRepo
;
import
com.example.calendar.repo.UserRepo
;
import
com.example.calendar.validator.EventValidator
;
import
com.example.calendar.validator.UserValidator
;
import
java.util.List
;
...
...
@@ -37,7 +39,8 @@ public class EventController {
@Autowired
CalendarRepo
calendarRepo
;
@Autowired
private
EventValidator
eventValidator
;
@GetMapping
(
value
=
"/add-new-event"
)
public
String
addNewEventGet
(
Model
models
,
Event
event
)
...
...
@@ -50,7 +53,7 @@ public class EventController {
@PostMapping
(
value
=
"/add-new-event"
)
public
String
addNewEventPost
(
@Valid
Event
event
,
BindingResult
bindingResult
)
{
eventValidator
.
validate
(
event
,
bindingResult
);
if
(
bindingResult
.
hasErrors
())
{
return
"addNewEvent"
;
}
...
...
src/main/java/com/example/calendar/models/Event.java
View file @
f0aee202
...
...
@@ -12,8 +12,6 @@ import javax.persistence.Id;
import
javax.persistence.JoinColumn
;
import
javax.persistence.ManyToOne
;
import
javax.persistence.Table
;
import
javax.validation.constraints.NotBlank
;
import
javax.validation.constraints.NotNull
;
import
org.springframework.format.annotation.DateTimeFormat
;
...
...
@@ -21,7 +19,6 @@ import org.springframework.format.annotation.DateTimeFormat;
@Table
(
name
=
"EventTable"
)
public
class
Event
{
@NotBlank
@Column
(
name
=
"Name"
)
private
String
name
;
@Column
(
name
=
"Description"
)
...
...
src/main/java/com/example/calendar/validator/EventValidator.java
View file @
f0aee202
package
com.example.calendar.validator
;
import
java.time.LocalDate
;
import
java.time.LocalTime
;
import
java.util.ArrayList
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.stereotype.Component
;
import
org.springframework.validation.Errors
;
import
org.springframework.validation.ValidationUtils
;
import
org.springframework.validation.Validator
;
import
com.example.calendar.models.Calendar
;
import
com.example.calendar.models.Event
;
import
com.example.calendar.models.User
;
import
com.example.calendar.repo.CalendarRepo
;
import
com.example.calendar.repo.EventRepo
;
import
com.example.calendar.repo.UserRepo
;
@Component
public
class
EventValidator
implements
Validator
{
@Autowired
EventRepo
eventRepo
;
@Autowired
CalendarRepo
calendarRepo
;
@Autowired
UserRepo
userRepo
;
@Override
public
boolean
supports
(
Class
<?>
aClass
)
{
return
Event
.
class
.
equals
(
aClass
);
...
...
@@ -25,11 +46,47 @@ public class EventValidator implements Validator {
// Date cannot be in the past
// Date & time cannot be the same
// Time cannot be in the past of current day
ValidationUtils
.
rejectIfEmptyOrWhitespace
(
errors
,
"name"
,
"NotEmpty"
);
if
(
e
.
getStartDate
()
==
null
)
{
errors
.
rejectValue
(
"startDate"
,
"DateEmpty"
);
return
;
}
if
(
e
.
getStartDate
().
isBefore
(
LocalDate
.
now
()))
{
errors
.
rejectValue
(
"startDate"
,
"DateInPast"
);
return
;
}
if
(
e
.
getStartDate
().
equals
(
LocalDate
.
now
())
&&
e
.
getStartTime
().
isBefore
(
LocalTime
.
now
()))
{
errors
.
rejectValue
(
"startDate"
,
"DateInPast"
);
return
;
}
//Get current user
Object
principal
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getPrincipal
();
String
username
=
""
;
if
(
principal
instanceof
UserDetails
)
{
username
=
((
UserDetails
)
principal
).
getUsername
();
}
else
{
username
=
principal
.
toString
();
}
User
currUser
=
userRepo
.
findByUsername
(
username
);
Calendar
c1
=
calendarRepo
.
findByUser
(
currUser
);
ArrayList
<
Event
>
allUserEvents
=
eventRepo
.
findAllByCalendar
(
c1
);
for
(
Event
event
:
allUserEvents
)
{
if
(
e
.
getStartDate
().
equals
(
event
.
getStartDate
())
&&
e
.
getStartTime
().
equals
(
event
.
getStartTime
()))
{
errors
.
rejectValue
(
"startDate"
,
"DateTaken"
);
break
;
}
}
}
}
\ No newline at end of file
src/main/resources/templates/addNewEvent.html
View file @
f0aee202
...
...
@@ -46,13 +46,14 @@ a:hover {
<br><br><br>
<form
action=
"#"
th:action=
"@{/add-new-event}"
th:object=
"${event}"
method=
"post"
>
<h2
class=
"form-signin-heading text-center"
>
Add an event
</h2><br>
<div
class=
"form-group"
th:classappend=
"${#fields.hasErrors('*')} ? 'has-error' : ''"
>
<input
name=
"name"
type=
"text"
th:field=
"*{name}"
class=
"form-control"
placeholder=
"Title"
autofocus=
"true"
/><br>
<p
th:if=
"${#fields.hasErrors('name')}"
th:errors=
"*{name}"
>
Name Error
</p>
<p
class=
"alert alert-danger"
th:if=
"${#fields.hasErrors('name')}"
th:errors=
"*{name}"
></p>
</div>
<textarea
name=
"description"
th:field=
"*{description}"
placeholder=
"Description..."
class=
"form-control"
rows=
"3"
></textarea>
<br><div
class=
"row"
>
<div
class=
"col-sm-5"
>
Date:
<input
id=
"datePicker"
type=
"date"
th:field=
"*{startDate}"
/>
</div>
<p
class=
"alert alert-danger"
th:if=
"${#fields.hasErrors('startDate')}"
th:errors=
"startDate"
></p>
<div
class=
"col-sm-5"
th:classappend=
"${#fields.hasErrors('*')} ? 'has-error' : ''"
>
Date:
<input
id=
"datePicker"
type=
"date"
th:field=
"*{startDate}"
/>
<p
class=
"alert alert-danger"
th:if=
"${#fields.hasErrors('startDate')}"
th:errors=
"
*{
startDate
}
"
></p>
</div>
<div
class=
"col-sm-4"
>
Time:
<input
id=
"timePicker"
type=
"time"
th:field=
"*{startTime}"
/></div>
<div
class=
"col-sm-3"
>
<div
class=
"form-check"
>
...
...
src/main/resources/validation.properties
View file @
f0aee202
...
...
@@ -4,4 +4,6 @@ Duplicate.userForm.username=Someone already has that username.
Size.userForm.password
=
Try one with at least 8 characters.
Diff.userForm.passwordConfirm
=
These passwords don't match.
NotBlank
=
This field cannot be empty!
DateInPast
=
Date cannot be in the past.
\ No newline at end of file
DateInPast
=
Date cannot be in the past.
DateEmpty
=
Date cannot be left empty.
DateTaken
=
You already have an event at this time!
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment