my code print error even the input is correct

Viewed 55

This is origin code

if (!isNullOrEmpty(configTypeBuilder.destinationField, RULE_CRITERIA_CONFIG_TYPE_BUILDER_DESTINATION_FIELD_PATH)) {
    if (ruleAttributes.firstOrNull { ruleAttribute -> ruleAttribute == destinationField } == null) {
        addValidationError(RULE_CRITERIA_CONFIG_TYPE_BUILDER_DESTINATION_FIELD_PATH, configTypeBuilder.destinationField, NOT_EXIST_CONSTRAINT)
    }
}

I modify the code, just add the invalid value to a list, and then print them, but now when I enter valid destinationField after invalid destinationField, it will still show the error message, I don't know what's going on

        val invalidDestination = hashSetOf<String>()

    if (!isNullOrEmpty(configTypeBuilder.destinationField, RULE_CRITERIA_CONFIG_TYPE_BUILDER_DESTINATION_FIELD_PATH)) {
        for (destinationField in destinationFieldList) {
            if (!ruleAttributes.contains(destinationField)) {
                invalidDestination.add(destinationField)
            }
        }
        if (invalidDestination.size > 0) {
            addValidationError(
                "$RULE_CRITERIA_CONFIG_TYPE_BUILDER_DESTINATION_FIELD_PATH $NOT_EXIST_CONSTRAINT",
                DESTINATION,
                "$ADD_TAX_ENGINE_ATTRIBUTE_FIELDS $invalidDestination"
            )
        }
    }
1 Answers

If your goal is to show error message only if there are no valid destinations, you need to add one more condition to your if statement. If not please clarify your question and post the whole function

Condition example:

    var foundValid = false
    for (destinationField in destinationFieldList) {
        if (!ruleAttributes.contains(destinationField)) {
            invalidDestination.add(destinationField)
        }else {
            foundValid = true
            break //if you need invalidDestination list for later remove this break
        }
    }
    if (invalidDestination.size > 0 && !foundValid) {
        ...
    }
Related