pglazkov / MvvmValidation

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

How to remove a rule #15

Closed maciz84 closed 7 years ago

maciz84 commented 7 years ago

Hi there,

How can I remove a specific rule. there is the Validator.RemoveRule function but do not know how to use it?

maciz84 commented 7 years ago

Please any help on this?

pglazkov commented 7 years ago

Sorry for a late reply. Indeed, you can use the Validator.RemoveRule method. You just need to pass the instance of the rule that you want to remove as an argument. You get that instance when you register a rule. Here is an example:

var myRule = Validator.AddRule(...);
Validator.RemoveRule(myRule);

Hope this helps.

gabecruz commented 7 years ago

I usually place the following in my view model base class.

private ValidationHelper Validator { get; }
private Dictionary<string, IList<ValidationTargetInfo>> ValidationTargets { get; set; }

public IValidationRule AddValidationRule(string targetName, Func<RuleResult> validateDelegate)
{
    var rule = Validator.AddRule(targetName, validateDelegate);

    if (ValidationTargets.ContainsKey(targetName))
    {
        var list = ValidationTargets[targetName];

        list?.Add(new ValidationTargetInfo(targetName, validateDelegate, rule));
    }
    else
    {
        ValidationTargets.Add(targetName, new List<ValidationTargetInfo> { new ValidationTargetInfo(targetName, validateDelegate, rule) });
    }

    return rule;
}

public void ResetValidation()
{
    Validator.Reset();
}

public void ResumeValidation()
{
    foreach (var validationTarget in ValidationTargets)
    {
        IList<ValidationTargetInfo> targetList;

        if (ValidationTargets.TryGetValue(validationTarget.Key, out targetList))
        {
            foreach (var target in targetList)
            {
                Validator.AddRule(target.TargetName, target.ValidateDelegate);
            }
        }
    }
}

public void ResumeValidation(string targetName)
{
    IList<ValidationTargetInfo> targetList;

    if (ValidationTargets.TryGetValue(targetName, out targetList))
    {
        foreach (var target in targetList)
        {
            Validator.AddRule(target.TargetName, target.ValidateDelegate);
        }
    }
}

public void SuspendValidation()
{
    Validator.RemoveAllRules();
}

public void SuspendValidation(string targetName)
{
    IList<ValidationTargetInfo> list;

    if (ValidationTargets.TryGetValue(targetName, out list))
    {
        foreach (var target in list)
        {
            Validator.RemoveRule(target.ValidationRule);
        }
    }
}

public Task<ValidationResult> Validate()
{
    return Validator.ValidateAllAsync();
}

public ValidationResult ValidateProperty([CallerMemberName] string targetName = null)
{
    return targetName == null ? default(ValidationResult) : Validator.Validate(targetName);
}

public Task<ValidationResult> ValidatePropertyAsync([CallerMemberName] string targetName = null)
{
    return targetName == null ? default(Task<ValidationResult>) : Validator.ValidateAsync(targetName);
}
pglazkov commented 7 years ago

@gabecruz I would like to note that if you need to suspend validation there is a built in ValidationHelper.SuppressValidation method that you can use for that, so there is no need to remove all the rules. If you have tried it and it didn't work for you, do you mind telling me that was the problem?

gabecruz commented 7 years ago

@pglazkov First of all let me say thank you for your work on this great little library. It has provided great benefit to me for a number of years.
As for the SuppressValidation method, yes you are right but I don't remember it allowing you to suppress validation on a single property does it?

pglazkov commented 7 years ago

@gabecruz Thanks! Glad that my library was useful for you :) And it is correct that SuppressValidation doesn't allow you to suppress validation for a single target. I'm wandering, what would be the scenarios when you would want to do that?

maciz84 commented 7 years ago

Thanks guys sorry for he late response but it worked. Thank you again for your help and I really appreciate such a great library. I use it all the time

gabecruz commented 7 years ago

@pglazkov there are situations in forms over data (lob) type of applications where depending on user input, validations for a target may need to be turned off. It's an edge case but it I am starting to see it more and more.

pglazkov commented 7 years ago

@gabecruz Thanks for the explanation. I created a separate issue for this. Will probably add this possibility soon.