* dynamic validation

* small fixes

* remove set property

* fix conflicts

* fix conflicts

* add optional

* small fix

* update get
This commit is contained in:
lor6 2017-04-19 15:27:02 +03:00 committed by Grzegorz Piwowarek
parent 54d24521af
commit 699cf9d77b
3 changed files with 16 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.dynamicvalidation;
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thymeleaf.util.StringUtils;
@ -12,27 +13,34 @@ import java.util.regex.Pattern;
public class ContactInfoValidator implements ConstraintValidator<ContactInfo, String> {
private static final Logger LOG = Logger.getLogger(ContactInfoValidator.class);
@Autowired
private ContactInfoExpressionRepository expressionRepository;
@Value("${contactInfoType}")
String expressionType;
private String pattern;
@Override
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
public boolean isValid(final String value, final ConstraintValidatorContext context) {
if (StringUtils.isEmptyOrWhitespace(expressionType)) {
return false;
if (!StringUtils.isEmptyOrWhitespace(pattern)) {
return Pattern.matches(pattern, value);
}
return expressionRepository
.findOne(expressionType)
.map(ContactInfoExpression::getPattern)
.map(p -> Pattern.matches(p, value))
.orElse(false);
LOG.error("Contact info pattern missing!");
return false;
}
}

View File

@ -1,10 +1,7 @@
package com.baeldung.dynamicvalidation.config;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

View File

@ -6,7 +6,6 @@ import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@ -16,11 +15,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
public class PersistenceConfig {
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();