class ValidAddressFormValidator implements ConstraintValidator<ValidAddressForm, AddressForm> {

    @Override
    public boolean isValid(AddressForm value, ConstraintValidatorContext context) {
        boolean valid;
        if (value.getAction().equals(AddressForm.Action.SAVE)) {
            if (value.isManualAddressEntry()) {
                valid = isAddressValid(value.getManualAddress(), context);
            } else if (value.isPostcodeAddressEntry()) {
                if (value.getSelectedPostcodeIndex() == null || value.getSelectedPostcodeIndex() < 0) {
                    updateConstraintValidatorContext(context, "{validation.standard.address.select.required}", "selectedPostcodeIndex");
                    valid = false;
                } else {
                    valid = true;
                }
            } else {
                updateConstraintValidatorContext(context, "{validation.standard.address.required}", "postcodeInput");
                valid = false;
            }
        } else if (value.getAction().equals(AddressForm.Action.SEARCH_POSTCODE) &&
            isNullOrEmpty(value.getPostcodeInput())) {
            updateConstraintValidatorContext(context, "{validation.standard.address.search.postcode.required}", "postcodeInput");
            valid = false;
        } else {
            valid = true;
        }
        return valid;

    }

}