Skip to content

Commit

Permalink
Bean Validation - groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kiteB committed Sep 27, 2021
1 parent dbe38c3 commit ca85fde
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
11 changes: 6 additions & 5 deletions validation/src/main/java/hello/itemservice/domain/item/Item.java
Expand Up @@ -12,17 +12,18 @@
//@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() {
Expand Down
@@ -0,0 +1,4 @@
package hello.itemservice.domain.item;

public interface SaveCheck {
}
@@ -0,0 +1,4 @@
package hello.itemservice.domain.item;

public interface UpdateCheck {
}
Expand Up @@ -2,6 +2,8 @@

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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -45,7 +47,7 @@ public String addForm(Model model) {
return "validation/v3/addForm";
}

@PostMapping("/add")
// @PostMapping("/add")
public String addItem(@Validated @ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes) {

//특정 필드 예외가 아닌 전체 예외
Expand All @@ -68,14 +70,37 @@ public String addItem(@Validated @ModelAttribute Item item, BindingResult bindin
return "redirect:/validation/v3/items/{itemId}";
}

@PostMapping("/add")
public String addItemV2(@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()) {
log.info("errors={}", bindingResult);
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) {
Item item = itemRepository.findById(itemId);
model.addAttribute("item", item);
return "validation/v3/editForm";
}

@PostMapping("/{itemId}/edit")
// @PostMapping("/{itemId}/edit")
public String edit(@PathVariable Long itemId, @Validated @ModelAttribute Item item, BindingResult bindingResult) {
//특정 필드 예외가 아닌 전체 예외
if (item.getPrice() != null && item.getQuantity() != null) {
Expand All @@ -94,5 +119,24 @@ public String edit(@PathVariable Long itemId, @Validated @ModelAttribute Item it
return "redirect:/validation/v3/items/{itemId}";
}

@PostMapping("/{itemId}/edit")
public String editV2(@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()) {
log.info("errors={}", bindingResult);
return "validation/v3/editForm";
}

itemRepository.update(itemId, item);
return "redirect:/validation/v3/items/{itemId}";
}

}

0 comments on commit ca85fde

Please sign in to comment.