Last active
June 22, 2020 08:55
-
-
Save Char0394/c45f16d4fcaf71dc02d64519f302b771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidatableObject<T> : IValidatable<T> | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
public List<IValidationRule<T>> Validations { get; } = new List<IValidationRule<T>>(); | |
public List<string> Errors { get; set; } = new List<string>(); | |
public bool CleanOnChange { get; set; } = true; | |
T _value; | |
public T Value | |
{ | |
get=>_value; | |
set | |
{ | |
_value = value; | |
if (CleanOnChange) | |
IsValid = true; | |
} | |
} | |
public bool IsValid { get; set; } = true; | |
public virtual bool Validate() | |
{ | |
Errors.Clear(); | |
IEnumerable<string> errors = Validations.Where(v => !v.Check(Value)) | |
.Select(v => v.ValidationMessage); | |
Errors = errors.ToList(); | |
IsValid = !Errors.Any(); | |
return this.IsValid; | |
} | |
public override string ToString() | |
{ | |
return $"{Value}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment