From 6207da8fa5a315f33e13ee2b24f9ffcf5b5685c6 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 9 Mar 2022 12:48:40 +0400 Subject: [PATCH] #1 -- add validation --- data/db.mv.db | Bin 24576 -> 28672 bytes .../ru/ulstu/controller/NewsController.java | 9 ++++++++- src/main/java/ru/ulstu/model/News.java | 3 +++ src/main/resources/public/css/main.css | 4 ++++ src/main/resources/templates/editNews.html | 5 ++++- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/data/db.mv.db b/data/db.mv.db index db9ad7d68730cb8f45d91a8f364aea9b6a28e9b4..f2a5f01bea579e0c6be94328e072eae670ce8703 100644 GIT binary patch literal 28672 zcmeHPO>g7I8J29zO}x9oszDkc>0y@HjlF>2@LMD=fh@6WVP$zM$=l7P#*iE(M!b@1 z%bP_HO;EJxp$O7T+ii>V&=$!b2$1v-6v!XQsVIt`+EZ^mG?xr#_%WiyU3nqbsqpGR zafTx22mk_r03dK-2y6rYzcB2fVL$*700aO5KmZT`1ONd*01yBK00BVYe}zC4 zsu{@e3KtCD|%*yEC zK5;ZdA-3+NsQ~n{M+bxY@N_V8WHk3rm{suN(offl1*K44x*d={K0O?jo~6~_TKe~; zqEM-XfUdl|`dQ(n!n5qAH&xY2_UvR2CwpFrg0QKqhFjHQ1&wWWwl&z+WLwO(`~?5< z0N*E@2ZMk-+|+sDaOgGg2w!4HXkiB*CHo!He}UBxkI0~Rj0Y#!AJIgL6MpIldxvCG z77LnKFrQ;5pALdyYxpKLH|(2DjelYOh3Jp^fT^K!f zKv9`Q3JZo|!LYPHy~r>#W0+{xcz?!b#e-Uj@$wm7YTr)r63m*hKRv-p=G{)#yHV)2 zPU5yvZc*YL3Smy}MDEWkY(OCHuKD}_l<1XhCF4&=mN0%qWdDpHS)y>Bo1th7z5VkPr}d_!Zju7Sl%fBp^mY*pos-_!ewe(D(ymAe_davvDP`%^UdhY(#UbEFesw|gR zBqUw=Ns-s1y{_9q&F`T0ZV$Qln%!O(N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ea>pqD{~On1&2y$aH#5)uQ*53;&4s#oSwLE-n?{#LvVV84 zxudVpeQL`oPNzlAW~&od@bJ=M$p5b-weZ2d#n_Ple{QV?cyJ&91b_e#00KbZStgMB S|GEFo`p{YZzpEZo{{LS??+2Iw diff --git a/src/main/java/ru/ulstu/controller/NewsController.java b/src/main/java/ru/ulstu/controller/NewsController.java index d76fe1f..61cca27 100644 --- a/src/main/java/ru/ulstu/controller/NewsController.java +++ b/src/main/java/ru/ulstu/controller/NewsController.java @@ -8,6 +8,7 @@ package ru.ulstu.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +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; @@ -15,6 +16,8 @@ import org.springframework.web.bind.annotation.PostMapping; import ru.ulstu.model.News; import ru.ulstu.service.NewsService; +import javax.validation.Valid; + @Controller public class NewsController { private final NewsService newsService; @@ -30,8 +33,12 @@ public class NewsController { } @PostMapping("saveNews") - public String saveNews(@ModelAttribute News news) { + public String saveNews(@Valid @ModelAttribute News news, BindingResult result) { + if (result.hasErrors()) { + return "editNews"; + } newsService.save(news); + return "redirect:/"; } } diff --git a/src/main/java/ru/ulstu/model/News.java b/src/main/java/ru/ulstu/model/News.java index 7efb6dc..af66265 100644 --- a/src/main/java/ru/ulstu/model/News.java +++ b/src/main/java/ru/ulstu/model/News.java @@ -2,17 +2,20 @@ package ru.ulstu.model; import javax.persistence.Entity; import javax.persistence.Lob; +import javax.validation.constraints.NotEmpty; import java.util.Date; @Entity public class News extends BaseEntity { private final static int MAX_NEWS_TEXT_PREVIEW_LENGTH = 800; + @NotEmpty(message = "Заголовок не может быть пустым") private String title; private Date date; @Lob + @NotEmpty(message = "Текст новости не может быть пустым") private String text; public News() { diff --git a/src/main/resources/public/css/main.css b/src/main/resources/public/css/main.css index c207eae..b2b570d 100644 --- a/src/main/resources/public/css/main.css +++ b/src/main/resources/public/css/main.css @@ -19,4 +19,8 @@ .news-image { width: 300px; +} + +.error { + color: red; } \ No newline at end of file diff --git a/src/main/resources/templates/editNews.html b/src/main/resources/templates/editNews.html index 96aa36e..a6df9fb 100644 --- a/src/main/resources/templates/editNews.html +++ b/src/main/resources/templates/editNews.html @@ -11,14 +11,17 @@

Редактирование новости:

-

+

+ Не может быть пустым

+

+ Не может быть пустым