Search in sources :

Example 1 with ValidationRule

use of com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRule in project connect-sdk-client-android by Ingenico-ePayments.

the class RenderValidationHelper method renderValidationMessageOnScreen.

private void renderValidationMessageOnScreen(ValidationErrorMessage validationResult, PaymentItem paymentItem) {
    String fieldId = validationResult.getPaymentProductFieldId();
    // Get the correct view of the invalid inputfield
    View view = parentView.findViewWithTag(fieldId);
    if (!canErrorMessageBeRendered(view, fieldId)) {
        return;
    }
    String validationMessage;
    if ("length".equals(validationResult.getErrorMessage()) && validationResult.getRule() instanceof ValidationRuleLength) {
        validationMessage = getCorrectMessageForLength(validationResult);
    } else {
        validationMessage = translator.getValidationMessage(validationResult.getErrorMessage());
    }
    if (validationResult.getRule() != null && paymentItem != null) {
        // Find the correct validationRule and format its message with variables attributes
        for (PaymentProductField field : paymentItem.getPaymentProductFields()) {
            if (field.getId().equals(validationResult.getPaymentProductFieldId())) {
                ValidationRule rule = validationResult.getRule();
                // if ValidationRuleLength, then add extra information to the errormessage
                if (rule instanceof ValidationRuleLength) {
                    // Replace the generic placeholders with Java specific placeholders, e.g. {maxlength} becomes {0}
                    validationMessage = formatMessagePlaceHolders(validationMessage);
                    // Show only the maxlength message if there is only one placeholder, else show minlenght and maxlength
                    if (validationMessage.split("\\{").length == 2) {
                        validationMessage = MessageFormat.format(validationMessage, ((ValidationRuleLength) rule).getMaxLength());
                    } else if (validationMessage.split("\\{").length == 3) {
                        validationMessage = MessageFormat.format(validationMessage, ((ValidationRuleLength) rule).getMinLength(), ((ValidationRuleLength) rule).getMaxLength());
                    }
                }
                // if ValidationRuleRange,then add extra information to the errormessage
                if (rule instanceof ValidationRuleRange) {
                    // Replace the generic placeholders with Java specific placeholders, e.g. {maxlength} becomes {0}
                    validationMessage = formatMessagePlaceHolders(validationMessage);
                    // Show only the maxvalue if there is only one placeholder, else show minvalue and maxvalue
                    if (validationMessage.split("\\{").length == 2) {
                        validationMessage = MessageFormat.format(validationMessage, ((ValidationRuleRange) rule).getMaxValue());
                    } else if (validationMessage.split("\\{").length == 3) {
                        validationMessage = MessageFormat.format(validationMessage, ((ValidationRuleRange) rule).getMinValue(), ((ValidationRuleRange) rule).getMaxValue());
                    }
                }
            }
        }
    }
    validationMessageRenderer.renderValidationMessage(validationMessage, (ViewGroup) view.getParent(), validationResult.getPaymentProductFieldId());
}
Also used : ValidationRuleLength(com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRuleLength) PaymentProductField(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentProductField) ValidationRuleRange(com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRuleRange) View(android.view.View) ValidationRule(com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRule)

Aggregations

View (android.view.View)1 PaymentProductField (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentProductField)1 ValidationRule (com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRule)1 ValidationRuleLength (com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRuleLength)1 ValidationRuleRange (com.globalcollect.gateway.sdk.client.android.sdk.model.validation.ValidationRuleRange)1