pglazkov / MvvmValidation

Lightweight library that helps reduce boilerplate when implementing validation in XAML MVVM applications
MIT License
167 stars 31 forks source link

Returning multiple failure results for a single target #9

Closed roverell closed 7 years ago

roverell commented 7 years ago

I'm looking to use this library to perform multiple validation checks, and returning/displaying any failures.

I notice you explicitly only return the first invalid rule (see below), and I understand that this is likely the most sensible response in most cases. However, would you consider providing an option on the constructor or as a property to bypass this and return all results?

            // Skip rule if the target is already invalid
            if (failedTargets.Contains(rule.Target))
            {
                // Assume that the rule is valid at this point because we are not interested in this error until
                // previous rule is fixed.
                SaveRuleValidationResultAndNotifyIfNeeded(rule, RuleResult.Valid(), syncContext);

                return TaskEx.FromResult(true);
            }
pglazkov commented 7 years ago

You can now do this either globally by providing a settings object to the constructor of ValidationHelper like this:

Validator = new ValidationHelper(new ValidationSettings
{
    DefaultRuleSettings = new ValidationRuleSettings
    {
        ExecuteOnAlreadyInvalidTarget = true
    }
});

Or by changing the settings of a specific validation rule with the new WithSettings method:

Validator.AddAsyncRule(...).WithSettings(s => s.ExecuteOnAlreadyInvalidTarget = false);

@roverell You can install a pre-release NuGet package from here: https://www.nuget.org/packages/MvvmValidation/3.1.0-beta1. Please give it a try and let me know if it works for you.