Bael 736 (#1646)
* dynamic validation * small fixes * remove set property * fix conflicts * fix conflicts * add optional
This commit is contained in:
@@ -6,6 +6,8 @@ import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
|
||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||
@@ -15,17 +17,17 @@ public class ContactInfoValidator implements ConstraintValidator<ContactInfo, St
|
||||
@Autowired
|
||||
private ContactInfoExpressionRepository expressionRepository;
|
||||
|
||||
@Value("${contactInfoType}")
|
||||
String expressionType;
|
||||
|
||||
@Override
|
||||
public void initialize(final ContactInfo contactInfo) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
||||
String expressionType = System.getProperty("contactInfoType");
|
||||
System.out.println(expressionType);
|
||||
final ContactInfoExpression expression = expressionRepository.findOne(expressionType);
|
||||
if (expression != null) {
|
||||
final String pattern = expression.getPattern();
|
||||
if (!StringUtils.isEmptyOrWhitespace(expressionType)) {
|
||||
final String pattern = expressionRepository.findOne(expressionType).map(ContactInfoExpression::getPattern).orElse("");
|
||||
if (Pattern.matches(pattern, value))
|
||||
return true;
|
||||
}
|
||||
|
||||
-22
@@ -10,21 +10,13 @@ import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
|
||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||
import com.baeldung.dynamicvalidation.model.Customer;
|
||||
|
||||
@Controller
|
||||
public class CustomerController {
|
||||
|
||||
@Autowired
|
||||
private ContactInfoExpressionRepository expressionRepository;
|
||||
|
||||
@GetMapping("/customer")
|
||||
public String getCustomerPage(Model model) {
|
||||
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
|
||||
return "customer";
|
||||
}
|
||||
|
||||
@@ -35,20 +27,6 @@ public class CustomerController {
|
||||
} else {
|
||||
model.addAttribute("message", "The information is valid!");
|
||||
}
|
||||
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
|
||||
return "customer";
|
||||
}
|
||||
|
||||
@PostMapping("/updateContactInfoType")
|
||||
@ResponseBody
|
||||
public void updateContactInfoType(@RequestParam final String type) {
|
||||
System.setProperty("contactInfoType", type);
|
||||
}
|
||||
|
||||
@GetMapping("/contactInfoTypes")
|
||||
@ResponseBody
|
||||
public List<ContactInfoExpression> getContactInfoType(Model model) {
|
||||
return expressionRepository.findAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -1,9 +1,11 @@
|
||||
package com.baeldung.dynamicvalidation.dao;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||
|
||||
public interface ContactInfoExpressionRepository extends JpaRepository<ContactInfoExpression, String> {
|
||||
|
||||
public interface ContactInfoExpressionRepository extends Repository<ContactInfoExpression, String> {
|
||||
Optional<ContactInfoExpression> findOne(String id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user