diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index c725330..e3d5b2f 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -42,19 +44,37 @@ private StorageLocationRepository storageLocationRepository; // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ @PostMapping("/bookInsertConfirm") - public String bookInsertConfirm(BookInsert bookInsert,Model model) { + public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { + if(result.hasErrors()) { + model.addAttribute("categoryList",categoryRepository.findAll()); + if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + //そのカテゴリに属するサブカテゴリを持ってくる + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert", bookInsert); + return "bookInsert"; + + } System.out.println(bookInsert); bookInsertService.save(bookInsert); model.addAttribute("bookInsert",bookInsert); return "bookInsertConfirm"; } + //確認画面から「もどる」ボタンを押したら入力画面に戻る @PostMapping("/bookInsert") public String bookInsertBack(BookInsert bookInsert,Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); - model.addAttribute("subCategoryList",new ArrayList<>()); - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert",bookInsert); + if(bookInsert.getBookCategoryId() != null) { + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert",bookInsert); return "bookInsert"; } @@ -62,8 +82,7 @@ private StorageLocationRepository storageLocationRepository; public String changeCategory(BookInsert bookInsert,Model model) { //カテゴリプルダウンに表示するデータを取得 model.addAttribute("categoryList",categoryRepository.findAll()); - - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", bookInsert); diff --git a/src/main/java/com/example/demo/entity/BookInsert.java b/src/main/java/com/example/demo/entity/BookInsert.java index 9305b62..be1fd47 100644 --- a/src/main/java/com/example/demo/entity/BookInsert.java +++ b/src/main/java/com/example/demo/entity/BookInsert.java @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Transient; +import jakarta.validation.constraints.Pattern; import lombok.Data; @@ -27,6 +28,8 @@ public class BookInsert { @Column(name = "title") private String title; + @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$", + message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください") @Column(name = "isbn") private String isbn; diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 04a99d9..be4be18 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -26,7 +26,9 @@


- + +
+



@@ -69,7 +71,7 @@


- diff --git a/src/main/resources/templates/bookInsertConfirm.html b/src/main/resources/templates/bookInsertConfirm.html index 9a53f47..882221c 100644 --- a/src/main/resources/templates/bookInsertConfirm.html +++ b/src/main/resources/templates/bookInsertConfirm.html @@ -17,7 +17,7 @@ - +