springboot_validation : validator
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package hello.itemservice.web.validation;
|
||||
|
||||
import hello.itemservice.domain.item.Item;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
@Component
|
||||
public class ItemValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return Item.class.isAssignableFrom(clazz);
|
||||
// item == clazz
|
||||
// item == subItem
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(Object target, Errors errors) {
|
||||
Item item = (Item) target;
|
||||
|
||||
if (!StringUtils.hasText(item.getItemName())) {
|
||||
errors.rejectValue("itemName", "required");
|
||||
}
|
||||
if (item.getPrice() == null || item.getPrice() < 1000 || item.getPrice() > 1000000) {
|
||||
errors.rejectValue("price", "range", new Object[]{1000, 1000000}, null);
|
||||
}
|
||||
if (item.getQuantity() == null || item.getQuantity() >= 9999) {
|
||||
errors.rejectValue("quantity", "max", new Object[]{9999}, null);
|
||||
}
|
||||
if (item.getPrice() != null && item.getQuantity() != null ) {
|
||||
int resultPrice = item.getPrice() * item.getQuantity();
|
||||
if (resultPrice < 10000) {
|
||||
errors.reject("totalPriceMin");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
||||
public class ValidationItemControllerV2 {
|
||||
|
||||
private final ItemRepository itemRepository;
|
||||
private final ItemValidator itemValidator;
|
||||
|
||||
@GetMapping
|
||||
public String items(Model model) {
|
||||
@@ -141,7 +142,7 @@ public class ValidationItemControllerV2 {
|
||||
return "redirect:/validation/v2/items/{itemId}";
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
// @PostMapping("/add")
|
||||
public String addItemV4(@ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) {
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
@@ -178,6 +179,23 @@ public class ValidationItemControllerV2 {
|
||||
return "redirect:/validation/v2/items/{itemId}";
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public String addItemV5(@ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) {
|
||||
|
||||
itemValidator.validate(item, bindingResult);
|
||||
|
||||
// 검증 실패시
|
||||
if (bindingResult.hasErrors()) {
|
||||
return "validation/v2/addForm";
|
||||
}
|
||||
|
||||
// 검증 성공시
|
||||
Item savedItem = itemRepository.save(item);
|
||||
redirectAttributes.addAttribute("itemId", savedItem.getId());
|
||||
redirectAttributes.addAttribute("status", true);
|
||||
return "redirect:/validation/v2/items/{itemId}";
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/{itemId}/edit")
|
||||
public String editForm(@PathVariable Long itemId, Model model) {
|
||||
|
||||
Reference in New Issue
Block a user