Loading src/main/java/com/example/demo/controller/HelpPropositionController.java +31 −6 Original line number Diff line number Diff line Loading @@ -7,15 +7,22 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; 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.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.demo.model.HelpProposition; import com.example.demo.model.User; import com.example.demo.repository.HelpPropositionRepository; import com.example.demo.service.IUserService; import com.example.demo.validator.HelpPropositionValidator; import com.example.demo.validator.UserRegisterValidator; @Controller public class HelpPropositionController { Loading @@ -26,6 +33,9 @@ public class HelpPropositionController { @Autowired private IUserService userService; @Autowired private HelpPropositionValidator helpPropositionValidator; @GetMapping("/allHelpProposition") public String getAll(Map<String, Object> model) { model.put("helpPropositions", helpPropositionRepository.findAll()); Loading @@ -39,15 +49,30 @@ public class HelpPropositionController { } @PostMapping("/insertHelpProposition") public String insertHelpProposition(@ModelAttribute HelpProposition helpProposition, Model model, HttpSession session, Principal principal) { public ModelAndView insertHelpProposition(@ModelAttribute HelpProposition helpProposition, BindingResult bindingResult, ModelMap modelMap, HttpSession session, Principal principal) { ModelAndView modelAndView = new ModelAndView(); helpPropositionValidator.validate(helpProposition, bindingResult); 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); helpProposition.setMentor(user); helpPropositionRepository.save(helpProposition); return "formHelpProposition"; return new ModelAndView("redirect:" + "/formHelpProposition"); } modelAndView.addObject("helpProposition", new HelpProposition()); modelAndView.setViewName("formHelpProposition"); return modelAndView; } @GetMapping("/allPropositionByMentor") Loading src/main/java/com/example/demo/validator/HelpPropositionValidator.java 0 → 100644 +31 −0 Original line number Diff line number Diff line package com.example.demo.validator; import com.example.demo.model.HelpProposition; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; @Component public class HelpPropositionValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return HelpProposition.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { HelpProposition helpProposition = (HelpProposition) target; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "branch", "branch.empty", "You must enter a branch!"); if(helpProposition.getBranch().length() < 2 || helpProposition.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 src/main/resources/templates/formHelpProposition.html +10 −4 Original line number Diff line number Diff line Loading @@ -16,12 +16,18 @@ <header th:insert="fragments/nav.html :: nav"> </header> <div class="container"> <h1 class="teal-text text-darken-4">Create a help proposition</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="@{/insertHelpProposition}" th:object="${helpProposition}" method="post"> <p><strong>Branch <input type="text" th:field="*{branch}" /></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>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>TimeBegin <input type="text" class="timepicker" th:field="*{timeBegin}" required /></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>TimeBegin <input type="text" class="timepicker" th:field="*{timeEnd}" required /></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> </form> Loading Loading
src/main/java/com/example/demo/controller/HelpPropositionController.java +31 −6 Original line number Diff line number Diff line Loading @@ -7,15 +7,22 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; 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.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.demo.model.HelpProposition; import com.example.demo.model.User; import com.example.demo.repository.HelpPropositionRepository; import com.example.demo.service.IUserService; import com.example.demo.validator.HelpPropositionValidator; import com.example.demo.validator.UserRegisterValidator; @Controller public class HelpPropositionController { Loading @@ -26,6 +33,9 @@ public class HelpPropositionController { @Autowired private IUserService userService; @Autowired private HelpPropositionValidator helpPropositionValidator; @GetMapping("/allHelpProposition") public String getAll(Map<String, Object> model) { model.put("helpPropositions", helpPropositionRepository.findAll()); Loading @@ -39,15 +49,30 @@ public class HelpPropositionController { } @PostMapping("/insertHelpProposition") public String insertHelpProposition(@ModelAttribute HelpProposition helpProposition, Model model, HttpSession session, Principal principal) { public ModelAndView insertHelpProposition(@ModelAttribute HelpProposition helpProposition, BindingResult bindingResult, ModelMap modelMap, HttpSession session, Principal principal) { ModelAndView modelAndView = new ModelAndView(); helpPropositionValidator.validate(helpProposition, bindingResult); 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); helpProposition.setMentor(user); helpPropositionRepository.save(helpProposition); return "formHelpProposition"; return new ModelAndView("redirect:" + "/formHelpProposition"); } modelAndView.addObject("helpProposition", new HelpProposition()); modelAndView.setViewName("formHelpProposition"); return modelAndView; } @GetMapping("/allPropositionByMentor") Loading
src/main/java/com/example/demo/validator/HelpPropositionValidator.java 0 → 100644 +31 −0 Original line number Diff line number Diff line package com.example.demo.validator; import com.example.demo.model.HelpProposition; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; @Component public class HelpPropositionValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return HelpProposition.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { HelpProposition helpProposition = (HelpProposition) target; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "branch", "branch.empty", "You must enter a branch!"); if(helpProposition.getBranch().length() < 2 || helpProposition.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
src/main/resources/templates/formHelpProposition.html +10 −4 Original line number Diff line number Diff line Loading @@ -16,12 +16,18 @@ <header th:insert="fragments/nav.html :: nav"> </header> <div class="container"> <h1 class="teal-text text-darken-4">Create a help proposition</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="@{/insertHelpProposition}" th:object="${helpProposition}" method="post"> <p><strong>Branch <input type="text" th:field="*{branch}" /></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>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>TimeBegin <input type="text" class="timepicker" th:field="*{timeBegin}" required /></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>TimeBegin <input type="text" class="timepicker" th:field="*{timeEnd}" required /></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> </form> Loading