Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -12,27 +13,34 @@

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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand All @@ -16,11 +15,6 @@
@Configuration
public class PersistenceConfig {

@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}

@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
Expand Down