Задать вопрос
@artemfisher

Как привязать ошибки валидации к MessageSource?

Добрый день!
Работаю со Spring WebFlux (не в Boot).

Есть класс, для полей которого делаю валидацию
public class Catalog {
	
	private int id;
	
	@Length(min=3, message="catalog.name.short")
	@NotNull(message="catalog.name.null")
	private String name;
//...
}


В файле для MessageSource прописываю
catalog.name.short = Catalog name must be at least 3 characters long
catalog.name.null = Catalog name must be not null


в шаблоне Thymeleaf
<form method="post" th:action="@{/save_catalog}" enctype="multipart/form-data" th:object="${catalog}">
	
	<input type="hidden" th:field="*{id}">

	<label th:text="#{add_edit_catalog_name}"></label>
	<input type="text" th:field="*{name}"/>
	<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}"> name error</p>
	
	<label th:text="#{add_edit_catalog_image}"></label>
	<input type="file" th:field="*{image}"/>	
	
	<input type="submit" th:value="#{add_edit_catalog_save}"/>	
</form>


В результате при ошибке валидации выводит:
catalog.name.short или catalog.name.null.

Как сделать чтобы подгружалось из MessageSource?
  • Вопрос задан
  • 9 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы