Bael 736 (#1685)
* dynamic validation * small fixes * remove set property * fix conflicts * fix conflicts * add optional * small fix * update get
This commit is contained in:
parent
54d24521af
commit
699cf9d77b
@ -2,6 +2,7 @@ package com.baeldung.dynamicvalidation;
|
|||||||
|
|
||||||
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
|
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
|
||||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.thymeleaf.util.StringUtils;
|
import org.thymeleaf.util.StringUtils;
|
||||||
@ -12,27 +13,34 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class ContactInfoValidator implements ConstraintValidator<ContactInfo, String> {
|
public class ContactInfoValidator implements ConstraintValidator<ContactInfo, String> {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(ContactInfoValidator.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ContactInfoExpressionRepository expressionRepository;
|
private ContactInfoExpressionRepository expressionRepository;
|
||||||
|
|
||||||
@Value("${contactInfoType}")
|
@Value("${contactInfoType}")
|
||||||
String expressionType;
|
String expressionType;
|
||||||
|
|
||||||
|
private String pattern;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(final ContactInfo contactInfo) {
|
public void initialize(final ContactInfo contactInfo) {
|
||||||
|
if (StringUtils.isEmptyOrWhitespace(expressionType)) {
|
||||||
|
LOG.error("Contact info type missing!");
|
||||||
|
} else {
|
||||||
|
pattern = expressionRepository.findOne(expressionType)
|
||||||
|
.map(ContactInfoExpression::getPattern)
|
||||||
|
.orElse("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
||||||
if (StringUtils.isEmptyOrWhitespace(expressionType)) {
|
if (!StringUtils.isEmptyOrWhitespace(pattern)) {
|
||||||
return false;
|
return Pattern.matches(pattern, value);
|
||||||
}
|
}
|
||||||
|
LOG.error("Contact info pattern missing!");
|
||||||
return expressionRepository
|
return false;
|
||||||
.findOne(expressionType)
|
|
||||||
.map(ContactInfoExpression::getPattern)
|
|
||||||
.map(p -> Pattern.matches(p, value))
|
|
||||||
.orElse(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package com.baeldung.dynamicvalidation.config;
|
package com.baeldung.dynamicvalidation.config;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
@ -6,7 +6,6 @@ import org.springframework.boot.autoconfigure.domain.EntityScan;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
@ -16,11 +15,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class PersistenceConfig {
|
public class PersistenceConfig {
|
||||||
|
|
||||||
@Bean
|
|
||||||
public JdbcTemplate getJdbcTemplate() {
|
|
||||||
return new JdbcTemplate(dataSource());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user