#1 -- add validation
This commit is contained in:
parent
9c42cb677e
commit
6207da8fa5
BIN
data/db.mv.db
BIN
data/db.mv.db
Binary file not shown.
@ -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:/";
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -19,4 +19,8 @@
|
||||
|
||||
.news-image {
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
}
|
@ -11,14 +11,17 @@
|
||||
<div class="container" layout:fragment="content">
|
||||
<h3>Редактирование новости:</h3>
|
||||
<form action="#" th:action="@{/saveNews}" th:object="${news}" method="post">
|
||||
<p style="color:red" th:text="${error}"></p>
|
||||
<div class="form-group">
|
||||
<label for="title">Заголовок</label>
|
||||
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости">
|
||||
<p th:if="${#fields.hasErrors('title')}" th:class="${#fields.hasErrors('title')}? error">
|
||||
Не может быть пустым</p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="text">Текст новости</label>
|
||||
<textarea class="form-control" id="text" th:field="*{text}" placeholder="Текст новости"></textarea>
|
||||
<p th:if="${#fields.hasErrors('text')}" th:class="${#fields.hasErrors('text')}? error">
|
||||
Не может быть пустым</p>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user