Skip to content

Instantly share code, notes, and snippets.

@Char0394
Last active June 22, 2020 08:55
Show Gist options
  • Save Char0394/c45f16d4fcaf71dc02d64519f302b771 to your computer and use it in GitHub Desktop.
Save Char0394/c45f16d4fcaf71dc02d64519f302b771 to your computer and use it in GitHub Desktop.
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