diff --git a/springboot_validation/src/main/java/hello/itemservice/domain/item/Item.java b/springboot_validation/src/main/java/hello/itemservice/domain/item/Item.java index a9db78e6..40654161 100644 --- a/springboot_validation/src/main/java/hello/itemservice/domain/item/Item.java +++ b/springboot_validation/src/main/java/hello/itemservice/domain/item/Item.java @@ -12,17 +12,18 @@ import javax.validation.constraints.NotNull; //@ScriptAssert(lang = "javascript", script = "_this.price * _this.quantity >= 10000", message = "총 합은 10000원 이상 이다.") public class Item { + @NotNull(groups = UpdateCheck.class) // 수정 시 요구사항 추가 private Long id; - @NotBlank + @NotBlank(groups = {SaveCheck.class, UpdateCheck.class}) private String itemName; - @NotNull - @Range(min = 1000, max = 1000000) + @NotNull(groups = {SaveCheck.class, UpdateCheck.class}) + @Range(min = 1000, max = 1000000, groups = {SaveCheck.class, UpdateCheck.class}) private Integer price; - @NotNull - @Max(9999) + @NotNull(groups = {SaveCheck.class, UpdateCheck.class}) + @Max(value = 9999, groups = {SaveCheck.class}) private Integer quantity; public Item() { diff --git a/springboot_validation/src/main/java/hello/itemservice/domain/item/SaveCheck.java b/springboot_validation/src/main/java/hello/itemservice/domain/item/SaveCheck.java new file mode 100644 index 00000000..6e582d64 --- /dev/null +++ b/springboot_validation/src/main/java/hello/itemservice/domain/item/SaveCheck.java @@ -0,0 +1,4 @@ +package hello.itemservice.domain.item; + +public interface SaveCheck { +} diff --git a/springboot_validation/src/main/java/hello/itemservice/domain/item/UpdateCheck.java b/springboot_validation/src/main/java/hello/itemservice/domain/item/UpdateCheck.java new file mode 100644 index 00000000..786635bf --- /dev/null +++ b/springboot_validation/src/main/java/hello/itemservice/domain/item/UpdateCheck.java @@ -0,0 +1,4 @@ +package hello.itemservice.domain.item; + +public interface UpdateCheck { +} diff --git a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java index a75cacea..1d77796e 100644 --- a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java +++ b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV3.java @@ -2,6 +2,8 @@ package hello.itemservice.web.validation; import hello.itemservice.domain.item.Item; import hello.itemservice.domain.item.ItemRepository; +import hello.itemservice.domain.item.SaveCheck; +import hello.itemservice.domain.item.UpdateCheck; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -40,7 +42,7 @@ public class ValidationItemControllerV3 { return "validation/v3/addForm"; } - @PostMapping("/add") +// @PostMapping("/add") public String addItem(@Validated @ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) { if (item.getPrice() != null && item.getQuantity() != null ) { @@ -62,6 +64,27 @@ public class ValidationItemControllerV3 { return "redirect:/validation/v3/items/{itemId}"; } + @PostMapping("/add") + public String addItem2(@Validated(SaveCheck.class) @ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes) { + + if (item.getPrice() != null && item.getQuantity() != null ) { + int resultPrice = item.getPrice() * item.getQuantity(); + if (resultPrice < 10000) { + bindingResult.reject("totalPriceMin", new Object[]{10000, resultPrice}, null); + } + } + + // 검증 실패시 + if (bindingResult.hasErrors()) { + return "validation/v3/addForm"; + } + + // 검증 성공시 + Item savedItem = itemRepository.save(item); + redirectAttributes.addAttribute("itemId", savedItem.getId()); + redirectAttributes.addAttribute("status", true); + return "redirect:/validation/v3/items/{itemId}"; + } @GetMapping("/{itemId}/edit") public String editForm(@PathVariable Long itemId, Model model) { @@ -70,11 +93,38 @@ public class ValidationItemControllerV3 { return "validation/v3/editForm"; } - @PostMapping("/{itemId}/edit") - public String edit(@PathVariable Long itemId, @ModelAttribute Item item) { +// @PostMapping("/{itemId}/edit") + public String edit(@PathVariable Long itemId, + @Validated @ModelAttribute Item item, BindingResult bindingResult) { + if (item.getPrice() != null && item.getQuantity() != null ) { + int resultPrice = item.getPrice() * item.getQuantity(); + if (resultPrice < 10000) { + bindingResult.reject("totalPriceMin", new Object[]{10000, resultPrice}, null); + } + } + + if (bindingResult.hasErrors()) { + return "validation/v3/editForm"; + } itemRepository.update(itemId, item); return "redirect:/validation/v3/items/{itemId}"; } + @PostMapping("/{itemId}/edit") + public String edit2(@PathVariable Long itemId, + @Validated(UpdateCheck.class) @ModelAttribute Item item, BindingResult bindingResult) { + if (item.getPrice() != null && item.getQuantity() != null ) { + int resultPrice = item.getPrice() * item.getQuantity(); + if (resultPrice < 10000) { + bindingResult.reject("totalPriceMin", new Object[]{10000, resultPrice}, null); + } + } + + if (bindingResult.hasErrors()) { + return "validation/v3/editForm"; + } + itemRepository.update(itemId, item); + return "redirect:/validation/v3/items/{itemId}"; + } } diff --git a/springboot_validation/src/main/resources/templates/validation/v3/editForm.html b/springboot_validation/src/main/resources/templates/validation/v3/editForm.html index 637ba550..8599ac57 100644 --- a/springboot_validation/src/main/resources/templates/validation/v3/editForm.html +++ b/springboot_validation/src/main/resources/templates/validation/v3/editForm.html @@ -8,6 +8,10 @@ .container { max-width: 560px; } + .field-error { + border-color: #dc3545; + color: #dc3545; + } @@ -19,28 +23,42 @@
+ +
+

글로벌 오류 메시지

+
+
- + +
+ 상품명 오류 +
- + +
+ 가격 오류 +
- + +
+ 수량 오류 +

- +