All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nls.validation.PostcodeValidator Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.nls.validation;

import com.nls.util.PostcodeUtil;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PostcodeValidator implements ConstraintValidator {
    private String postcodeField;
    private String countryField;
    private String countryValue;

    public void initialize(Postcode parameters) {
        this.countryField = parameters.countryField();
        this.countryValue = parameters.countryValue();
        this.postcodeField = parameters.postcodeField();
    }

    public boolean isValid(Object object, ConstraintValidatorContext context) {
        if (object == null) {
            return true;
        }

        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(object);
        if (countryField != null && !countryField.isEmpty()) {
            String country = String.valueOf(wrapper.getPropertyValue(countryField));
            if (country != null && !country.equals(countryValue)) {
                return true;
            }
        }

        String postcode = (String) wrapper.getPropertyValue(postcodeField);
        if (postcode == null || postcode.isEmpty()) {
            return true;
        }

        if (!PostcodeUtil.valid(postcode)) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
                    .addPropertyNode(postcodeField)
                    .addConstraintViolation();
            return false;
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy