Closed maciz84 closed 7 years ago
Please any help on this?
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.
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);
}
@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?
@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?
@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?
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
@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.
Hi there,
How can I remove a specific rule. there is the Validator.RemoveRule function but do not know how to use it?