Żeby nie śmiecić na forum to zadam tu jeszcze jedno pytanie.
Teraz mam problem taki, że w thymeleaf nie wyświetlają mi się komunikaty o błędach.
W thymeleaft wygląda tak:
<form th:action="@{/addadvert}" th:object="${advert}" method="post">
<input th:field="*{title}" type="text" placeholder="title" autocomplete="off" th:errorclass="invalid"/>
<div th:if="${#fields.hasErrors('title')}" class="red-text" th:errors="*{title}">Error</div>
<input th:field="*{description}" type="text" placeholder="description" autocomplete="off"/>
<div th:if="${#fields.hasErrors('description')}" class="red-text" th:errors="*{description}">Error</div>
<button type="submit" class="btn blue lighten-1">Add advert</button>
</form>
A metoda obsługująca formularz tak:
@PostMapping(value = "/addadvert")
public String addAdvert(
@Valid @ModelAttribute Advert advert,
BindingResult bindingResult,
RedirectAttributes redirectAttributes,
Principal principal
) {
if (bindingResult.hasErrors()) {
for (int i = 0; i < bindingResult.getAllErrors().size(); i++) {
log.info(bindingResult.getAllErrors().get(i).getObjectName());
log.info(bindingResult.getAllErrors().get(i).getCode());
log.info(bindingResult.getAllErrors().get(i).toString());
}
redirectAttributes.addFlashAttribute(
"org.springframework.validation.BindingResult.advert",
advert);
redirectAttributes.addFlashAttribute("advert", advert);
return "redirect:/addadvert";
}
User user = userService.findUserByUsername(principal.getName());
advert.setUser(user);
advertRepository.save(advert);
return "redirect:/userpanel";
}
Dodałem logi, żeby zobaczyć czy coś się dzieje i niby tak bo dostaję coś takiego:
2017-09-13 12:20:34.650 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : advert
2017-09-13 12:20:34.651 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : NotEmpty
2017-09-13 12:20:34.651 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : Field error in object 'advert' on field 'description': rejected value []; codes [NotEmpty.advert.description,NotEmpty.description,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [advert.description,description]; arguments []; default message [description]]; default message [may not be empty]
2017-09-13 12:20:34.651 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : advert
2017-09-13 12:20:34.651 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : Size
2017-09-13 12:20:34.651 INFO 2784 --- [nio-8080-exec-2] c.ad.web.controller.AddAdvertController : Field error in object 'advert' on field 'title': rejected value [as]; codes [Size.advert.title,Size.title,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [advert.title,title]; arguments []; default message [title],30,5]; default message [size must be between 5 and 30]
Próbowałem też bez dodawania redirectAttributes.addFlashAttribute
, a komunikaty wyświetlać tak:
<ul th:if="${#fields.hasErrors('*')}" class="errorlist">
<li th:each="err : ${#fields.errors('*')}" th:text="${err}">Błędne dane</li>
</ul>
ale efekt też był zerowy.