Ilhasoft / data-binding-validator

Android fields validation library based on data binding adapters.
Apache License 2.0
344 stars 53 forks source link

ignored validation when view is invisible or gone #28

Open manandhar-aarya opened 6 years ago

joneserick commented 6 years ago

@manandhar-aarya thanks a lot for your contribution. It works as expected on singular validations but It doesn't when trying to use multiple validations.

manandhar-aarya commented 6 years ago

@jonoid Can you explain what you mean by singular and multiple validations? I tried using multiple validation rules (as validateEmpty, validateMinLength, validateMaxLength) in one view and it still seems to work as expected.

joneserick commented 6 years ago

Oh, sorry for not being clear in my words. The fact is that there is nothing wrong with your solution. I found that the hidden view validation doesn't work by calling Validation's validate(List<ViewType> views) method.

public <ViewType extends View> boolean validate(List<ViewType> views) {
    List<View> viewWithValidations = getViewsWithValidation(views);
    return isAllViewsValid(viewWithValidations);
}

private boolean isAllViewsValid(List<View> viewWithValidations) {
    boolean allViewsValid = true;
    for (View viewWithValidation : viewWithValidations) {
        boolean viewValid = true;
        List<Rule> rules = (List) viewWithValidation.getTag(R.id.validator_rule);
        for (Rule rule : rules) {
            viewValid = viewValid && isRuleValid(rule);
            allViewsValid = allViewsValid && viewValid;
        }

        if(mode == FIELD_VALIDATION_MODE && !viewValid) {
            break;
        }
    }
    return allViewsValid;
}

But it only occurs when you by mistake includes a hidden view to be validated. In this case the hidden view should be included on the disabledViews list, so it will return true on the rule verification:

private boolean isRuleValid(Rule rule) {
    return disabledViews.contains(rule.getView()) || rule.validate();
}

The crazy thing is that it should work because if disabledViews list doesn't contain the respective view, the rule's validate() method should handle the visibility property. Maybe I'm missing something.