Bael 736 (#1646)
* dynamic validation * small fixes * remove set property * fix conflicts * fix conflicts * add optional
This commit is contained in:
parent
a5879bf8b7
commit
2b8ba178e4
@ -6,6 +6,8 @@ import javax.validation.ConstraintValidator;
|
|||||||
import javax.validation.ConstraintValidatorContext;
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.dao.ContactInfoExpressionRepository;
|
||||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||||
@ -15,17 +17,17 @@ public class ContactInfoValidator implements ConstraintValidator<ContactInfo, St
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ContactInfoExpressionRepository expressionRepository;
|
private ContactInfoExpressionRepository expressionRepository;
|
||||||
|
|
||||||
|
@Value("${contactInfoType}")
|
||||||
|
String expressionType;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(final ContactInfo contactInfo) {
|
public void initialize(final ContactInfo contactInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
||||||
String expressionType = System.getProperty("contactInfoType");
|
if (!StringUtils.isEmptyOrWhitespace(expressionType)) {
|
||||||
System.out.println(expressionType);
|
final String pattern = expressionRepository.findOne(expressionType).map(ContactInfoExpression::getPattern).orElse("");
|
||||||
final ContactInfoExpression expression = expressionRepository.findOne(expressionType);
|
|
||||||
if (expression != null) {
|
|
||||||
final String pattern = expression.getPattern();
|
|
||||||
if (Pattern.matches(pattern, value))
|
if (Pattern.matches(pattern, value))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -10,21 +10,13 @@ import org.springframework.ui.Model;
|
|||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
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;
|
import com.baeldung.dynamicvalidation.model.Customer;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class CustomerController {
|
public class CustomerController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ContactInfoExpressionRepository expressionRepository;
|
|
||||||
|
|
||||||
@GetMapping("/customer")
|
@GetMapping("/customer")
|
||||||
public String getCustomerPage(Model model) {
|
public String getCustomerPage(Model model) {
|
||||||
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
|
|
||||||
return "customer";
|
return "customer";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,20 +27,6 @@ public class CustomerController {
|
|||||||
} else {
|
} else {
|
||||||
model.addAttribute("message", "The information is valid!");
|
model.addAttribute("message", "The information is valid!");
|
||||||
}
|
}
|
||||||
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
|
|
||||||
return "customer";
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package com.baeldung.dynamicvalidation.dao;
|
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;
|
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);
|
||||||
}
|
}
|
||||||
|
@ -44,3 +44,5 @@ servlet.mapping=/dispatcherExampleURL
|
|||||||
#banner.image.height= //TODO
|
#banner.image.height= //TODO
|
||||||
#banner.image.margin= //TODO
|
#banner.image.margin= //TODO
|
||||||
#banner.image.invert= //TODO
|
#banner.image.invert= //TODO
|
||||||
|
|
||||||
|
contactInfoType=email
|
@ -3,39 +3,14 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
||||||
<title>Customer Page</title>
|
<title>Customer Page</title>
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
|
||||||
<script>
|
|
||||||
$(document).ready(function() {
|
|
||||||
$.ajax({
|
|
||||||
url: "contactInfoTypes"
|
|
||||||
}).done(function(data) {
|
|
||||||
var contactInfoType = $('#contactInfoType').text();
|
|
||||||
$.each(data,function(key, value) {
|
|
||||||
var o = new Option(value.type, value.type);
|
|
||||||
if (value.type == contactInfoType) {
|
|
||||||
o.selected = true;
|
|
||||||
}
|
|
||||||
$('#expressions').append(o);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
$('#expressions').change(function(){
|
|
||||||
$.ajax({
|
|
||||||
url: "updateContactInfoType",
|
|
||||||
method:'POST',
|
|
||||||
data:{type:$('#expressions').val()}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<br />
|
<br />
|
||||||
<form action="customer" method="POST">
|
<form action="customer" method="POST">
|
||||||
Contact Info: <input type="text" name="contactInfo" /> <br />
|
Contact Info: <input type="text" name="contactInfo" /> <br />
|
||||||
Contact Info Type: <select id="expressions" ></select> <br />
|
|
||||||
<input type="submit" value="Submit" />
|
<input type="submit" value="Submit" />
|
||||||
</form>
|
</form>
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<span th:text="${message}"></span><br />
|
<span th:text="${message}"></span><br />
|
||||||
<span id="contactInfoType" th:text="${contactInfoType}" style="visibility:hidden"></span><br />
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user