|
| 1 | +package com.baeldung.dynamicvalidation.config; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.validation.Valid; |
| 6 | + |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.stereotype.Controller; |
| 9 | +import org.springframework.ui.Model; |
| 10 | +import org.springframework.validation.BindingResult; |
| 11 | +import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestParam; |
| 14 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 15 | +import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository; |
| 16 | +import com.baeldung.dynamicvalidation.model.ContactInfoExpression; |
| 17 | +import com.baeldung.dynamicvalidation.model.Customer; |
| 18 | + |
| 19 | +@Controller |
| 20 | +public class CustomerController { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private ContactInfoExpressionRepository expressionRepository; |
| 24 | + |
| 25 | + @GetMapping("/customer") |
| 26 | + public String getCustomerPage(Model model) { |
| 27 | + model.addAttribute("contactInfoType", System.getProperty("contactInfoType")); |
| 28 | + return "customer"; |
| 29 | + } |
| 30 | + |
| 31 | + @PostMapping("/customer") |
| 32 | + public String validateCustomer(@Valid final Customer customer, final BindingResult result, final Model model) { |
| 33 | + if (result.hasErrors()) { |
| 34 | + model.addAttribute("message", "The information is invalid!"); |
| 35 | + } else { |
| 36 | + model.addAttribute("message", "The information is valid!"); |
| 37 | + } |
| 38 | + model.addAttribute("contactInfoType", System.getProperty("contactInfoType")); |
| 39 | + return "customer"; |
| 40 | + } |
| 41 | + |
| 42 | + @PostMapping("/updateContactInfoType") |
| 43 | + @ResponseBody |
| 44 | + public void updateContactInfoType(@RequestParam final String type) { |
| 45 | + System.setProperty("contactInfoType", type); |
| 46 | + } |
| 47 | + |
| 48 | + @GetMapping("/contactInfoTypes") |
| 49 | + @ResponseBody |
| 50 | + public List<ContactInfoExpression> getContactInfoType(Model model) { |
| 51 | + return expressionRepository.findAll(); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments