Spring boot & Thymeleafで@DateTimeFormatを使ってエラーメッセージを出力する方法
springの@DateTimeFormatを使ってactorForm.javaでvalidationしたいのですがmessageという要素が無いが為にエラーページが出力されるだけでエラーメッセージが出力されません。
いろいろと調べたのですがやり方がわかりませんでした。
良ければ教えてもらますか?
これがactorFormです
//@Pattern(regexp="(\\d{4}/\\d{2}/\\d{2})", message="{actor.validation.birthday}")
@DateTimeFormat(pattern = "yyyy/MM/dd")
private String birthday;
これがControllerです
@GetMapping("/actor/create")
public String create(ActorForm form, Model model) {
List<Prefecture> pref = prefectureRepository.findAll();
model.addAttribute("pref", pref);
return "Actor/create";
}
@PostMapping("/actor/save")
public String save(@Validated @ModelAttribute ActorForm form, BindingResult result, Model model, Locale locale) {
if (result.hasErrors()) {
String message = msg.getMessage("actor.validation.error", null, locale);
model.addAttribute("errorMessage", message);
return create(form, model);
}
Actor actor = convert(form);
actor = actorRepository.saveAndFlush(actor);
return "redirect:/actor/" + actor.getId().toString();
}
/**
* convert form to model.
*/
private Actor convert(ActorForm form) {
Actor actor = new Actor();
actor.setName(form.getName());
if (StringUtils.isNotEmpty(form.getHeight())) {
actor.setHeight(Integer.valueOf(form.getHeight()));
}
if (StringUtils.isNotEmpty(form.getBlood())) {
actor.setBlood(form.getBlood());
}
if (StringUtils.isNotEmpty(form.getBirthday())) {
DateTimeFormatter withoutZone = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime parsed = LocalDateTime.parse(form.getBirthday() + " 00:00:00", withoutZone);
Instant instant = parsed.toInstant(ZoneOffset.ofHours(9));
actor.setBirthday(Date.from(instant));
}
if (StringUtils.isNotEmpty(form.getBirthplaceId())) {
actor.setBirthplaceId(Integer.valueOf(form.getBirthplaceId()));
}
actor.setUpdateAt(new Date());
return actor;
}
propertiesファイル
# @DateTimeFormat
typeMismatch= is invalid.
typeMismatch.int= must be an integer.
typeMismatch.double= must be a double.
typeMismatch.float= must be a float.
typeMismatch.long= must be a long.
typeMismatch.short= must be a short.
typeMismatch.actorForm.java.lang.Integer= must be an integer.
typeMismatch.actorForm.java.lang.Double= must be a double.
typeMismatch.actorForm.java.lang.Float= must be a float.
typeMismatch.actorForm.java.lang.Long= must be a long.
typeMismatch.actorForm.java.lang.Short= must be a short.
typeMismatch.actorForm.java.util.Date= is not a date.
typeMismatch.actorForm.birthday=aaaaaaaaaa
typeMismatch.birthday=aaaaaaaa
htmlファイル
<form role="form" action="/actor/save" th:action="@{/actor/save}" th:object="${actorForm}"
method="post">
<!-- birthday -->
<div class="form-group">
<label for="id_birthday" class="col-sm-2 control-label" th:utext="#{actor.birthday}">birthday</label>
<div class="col-sm-12">
<input id="id_birthday" class="form-control" type="text" name="birthday" th:field="*{birthday}" th:placeholder="#{actor.create.birthday}"/>
<span th:if="${#fields.hasErrors('birthday')}" th:errors="*{birthday}" class="help-block">error!</span>
</div>
</div>