Skip to content
Snippets Groups Projects
Commit b450342a authored by ourfbht's avatar ourfbht
Browse files

validator for HelpRequest done

parent deed98f7
No related branches found
No related tags found
No related merge requests found
...@@ -9,15 +9,19 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -9,15 +9,19 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.model.HelpRequest; import com.example.demo.model.HelpRequest;
import com.example.demo.model.User; import com.example.demo.model.User;
import com.example.demo.repository.HelpRequestRepository; import com.example.demo.repository.HelpRequestRepository;
import com.example.demo.service.IUserService; import com.example.demo.service.IUserService;
import com.example.demo.validator.HelpRequestValidator;
@Controller @Controller
public class HelpRequestController { public class HelpRequestController {
...@@ -27,6 +31,9 @@ public class HelpRequestController { ...@@ -27,6 +31,9 @@ public class HelpRequestController {
@Autowired @Autowired
private IUserService userService; private IUserService userService;
@Autowired
private HelpRequestValidator helpRequestValidator;
@GetMapping("/allHelpRequest") @GetMapping("/allHelpRequest")
public String getAll(Map<String, Object> model) { public String getAll(Map<String, Object> model) {
...@@ -41,14 +48,31 @@ public class HelpRequestController { ...@@ -41,14 +48,31 @@ public class HelpRequestController {
} }
@PostMapping("/insertHelpRequest") @PostMapping("/insertHelpRequest")
public String insertHelpRequest(@ModelAttribute HelpRequest helpRequest, Model model, HttpSession session, Principal principal) { public ModelAndView insertHelpRequest(@ModelAttribute HelpRequest helpRequest, BindingResult bindingResult, ModelMap modelMap, HttpSession session, Principal principal) {
String email = SecurityContextHolder.getContext().getAuthentication().getName(); ModelAndView modelAndView = new ModelAndView();
User user = userService.findByEmail(email);
helpRequest.setPoulain(user);
helpRequestRepository.save(helpRequest); helpRequestValidator.validate(helpRequest, bindingResult);
return "formHelpRequest";
if(bindingResult.hasErrors()) {
modelAndView.addObject("registerMessage", "Registration failed: correct the fields !");
modelMap.addAttribute("bindingResult", bindingResult);
}
else {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
User user = userService.findByEmail(email);
helpRequest.setPoulain(user);
helpRequestRepository.save(helpRequest);
return new ModelAndView("redirect:" + "/formHelpRequest");
}
modelAndView.addObject("helpRequest", new HelpRequest());
modelAndView.setViewName("formHelpRequest");
return modelAndView;
} }
@GetMapping("/allRequestByPoulain") @GetMapping("/allRequestByPoulain")
public String getallPropositionByPoulain(Map<String, Object> model, HttpSession session, Principal principal) { public String getallPropositionByPoulain(Map<String, Object> model, HttpSession session, Principal principal) {
String email = SecurityContextHolder.getContext().getAuthentication().getName(); String email = SecurityContextHolder.getContext().getAuthentication().getName();
......
package com.example.demo.validator;
import com.example.demo.model.HelpRequest;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@Component
public class HelpRequestValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return HelpRequest.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
HelpRequest helpRequest = (HelpRequest) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "branch", "branch.empty", "You must enter a branch!");
if(helpRequest.getBranch().length() < 2 || helpRequest.getBranch().length() > 32){
errors.rejectValue("branch", "branch.size", "The size must be between 2 and 32");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "timeBegin", "timeBegin.empty", "You must enter a time begin!");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "timeEnd", "timeEnd.empty", "You must enter a time end!");
}
}
\ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<script src = "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.js"></script>
<title>Admin page</title> <title>All Help Proposition</title>
</head> </head>
<body> <body>
......
...@@ -25,9 +25,9 @@ ...@@ -25,9 +25,9 @@
<form action="#" th:action="@{/insertHelpProposition}" th:object="${helpProposition}" method="post"> <form action="#" th:action="@{/insertHelpProposition}" th:object="${helpProposition}" method="post">
<p><strong>Branch <input type="text" th:field="*{branch}" name="branch" pattern=".{2,32}" required title="2 to 32 characters" /></strong></p> <p><strong>Branch <input type="text" th:field="*{branch}" name="branch" pattern=".{2,32}" required title="2 to 32 characters" /></strong></p>
<p><strong>DateBegin <input type="datetime-local" class="datepicker" th:field="*{{dateBegin}}" name="dateBegin" /> </strong></p> <p><strong>DateBegin <input type="datetime-local" class="datepicker" th:field="*{{dateBegin}}" name="dateBegin" /> </strong></p>
<p><strong>TimeBegin <input type="text" class="timepicker" th:field="*{timeBegin}" required /></strong></p> <p><strong>TimeBegin <input type="text" class="timepicker" th:field="*{timeBegin}" name="timeBegin" required /></strong></p>
<p><strong>DateEnd <input type="datetime-local" class="datepicker" th:field="*{{dateEnd}}" /></strong> </p> <p><strong>DateEnd <input type="datetime-local" class="datepicker" th:field="*{{dateEnd}}" /></strong> </p>
<p><strong>TimeBegin <input type="text" class="timepicker" th:field="*{timeEnd}" required /></strong></p> <p><strong>TimeBegin <input type="text" class="timepicker" th:field="*{timeEnd}" name="timeEnd" required /></strong></p>
<p><strong>Comment <input type="text" th:field="*{comment}" /></strong></p> <p><strong>Comment <input type="text" th:field="*{comment}" /></strong></p>
<p><input type="submit" value="Submit" class="waves-effect waves-light btn right teal darken-2"/></p> <p><input type="submit" value="Submit" class="waves-effect waves-light btn right teal darken-2"/></p>
</form> </form>
......
...@@ -16,12 +16,18 @@ ...@@ -16,12 +16,18 @@
<header th:insert="fragments/nav.html :: nav"> </header> <header th:insert="fragments/nav.html :: nav"> </header>
<div class="container"> <div class="container">
<h1 class="teal-text text-darken-4">Create a help request</h1> <h1 class="teal-text text-darken-4">Create a help request</h1>
<div class="card-panel red lighten-4 red-text text-darken-4" th:if="${(bindingResult != null && bindingResult.getAllErrors() != null)}">
<span th:utext="${registerMessage}"></span>
<ul th:each="data : ${bindingResult.getAllErrors()}">
<li th:text="${data.getDefaultMessage()}"></li>
</ul>
</div>
<form action="#" th:action="@{/insertHelpRequest}" th:object="${helpRequest}" method="post"> <form action="#" th:action="@{/insertHelpRequest}" th:object="${helpRequest}" method="post">
<p><strong>Branch: <input type="text" th:field="*{branch}" /></strong></p> <p><strong>Branch: <input type="text" th:field="*{branch}" name="branch" pattern=".{2,32}" required title="2 to 32 characters" /></strong></p>
<p><strong>DateBegin <input type="datetime-local" class="datepicker" th:field="*{{dateBegin}}" /> </strong></p> <p><strong>DateBegin <input type="datetime-local" class="datepicker" th:field="*{{dateBegin}}" /> </strong></p>
<p><strong>TimeBegin<input type="text" class="timepicker" th:field="*{timeBegin}" /></strong></p> <p><strong>TimeBegin<input type="text" class="timepicker" th:field="*{timeBegin}" name="timeBegin" required /></strong></p>
<p><strong>DateEnd<input type="datetime-local" class="datepicker" th:field="*{{dateEnd}}" /></strong> </p> <p><strong>DateEnd<input type="datetime-local" class="datepicker" th:field="*{{dateEnd}}" /></strong> </p>
<p><strong>TimeBegin<input type="text" class="timepicker" th:field="*{timeEnd}" /></strong></p> <p><strong>TimeEnd<input type="text" class="timepicker" th:field="*{timeEnd}" name="timeEnd" required /></strong></p>
<p><strong>Comment<input type="text" th:field="*{comment}" /></strong></p> <p><strong>Comment<input type="text" th:field="*{comment}" /></strong></p>
<p><input type="submit" value="Submit" class="waves-effect waves-light btn right teal darken-2"/></p> <p><input type="submit" value="Submit" class="waves-effect waves-light btn right teal darken-2"/></p>
</form> </form>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment