Add ValidatingModelBase.RecordPropertyError

This can be used to manually change the state of properties, without using
the IModelValidator stuff. This handles more niche use-cases which don't
fit the pattern established by the IModelValidator.
This commit is contained in:
Antony Male 2016-08-26 11:38:25 +01:00
parent 4532a9859d
commit 8ea889d749
1 changed files with 42 additions and 0 deletions

View File

@ -142,6 +142,48 @@ namespace Stylet
return !this.HasErrors;
}
/// <summary>
/// Record a property error (or clear an error on a property). You can use this independently of the validation done by <see cref="Validator"/>
/// </summary>
/// <param name="property">Name of the property to change the errors for (or <see cref="String.Empty"/> to change the errors for the whole model)</param>
/// <param name="errors">The new errors, or null to clear errors for this property</param>
protected virtual void RecordPropertyError<TProperty>(Expression<Func<TProperty>> property, string[] errors)
{
this.RecordPropertyError(property.NameForProperty(), errors);
}
/// <summary>
/// Record a property error (or clear an error on a property). You can use this independently of the validation done by <see cref="Validator"/>
/// </summary>
/// <param name="propertyName">Name of the property to change the errors for (or <see cref="String.Empty"/> to change the errors for the whole model)</param>
/// <param name="errors">The new errors, or null to clear errors for this property</param>
protected virtual void RecordPropertyError(string propertyName, string[] errors)
{
if (propertyName == null)
propertyName = String.Empty;
bool changed = false;
this.propertyErrorsLock.Wait();
try
{
string[] existingErrors;
if (!this.propertyErrors.TryGetValue(propertyName, out existingErrors) || !this.ErrorsEqual(errors, existingErrors))
{
this.propertyErrors[propertyName] = errors;
changed = true;
}
}
finally
{
this.propertyErrorsLock.Release();
}
if (changed)
{
this.OnValidationStateChanged(new[] { propertyName });
}
}
/// <summary>
/// Validate a single property synchronously, by name
/// </summary>