Browse Source

Allowed pass object as validation error

pull/2867/head
all.owing 7 years ago
parent
commit
78866d54ed
  1. 11
      src/Avalonia.Base/Data/Core/Plugins/IndeiValidationPlugin.cs
  2. 21
      src/Avalonia.Base/Data/DataValidationException.cs
  3. 24
      src/Avalonia.Controls/DataValidationErrors.cs

11
src/Avalonia.Base/Data/Core/Plugins/IndeiValidationPlugin.cs

@ -85,8 +85,9 @@ namespace Avalonia.Data.Core.Plugins
if (target != null)
{
var errors = target.GetErrors(_name)?
.Cast<String>()
.Where(x => x != null).ToList();
.Cast<object>()
.Where(x => x != null)
.ToList();
if (errors?.Count > 0)
{
@ -100,16 +101,16 @@ namespace Avalonia.Data.Core.Plugins
return new BindingNotification(value);
}
private Exception GenerateException(IList<string> errors)
private Exception GenerateException(IList<object> errors)
{
if (errors.Count == 1)
{
return new Exception(errors[0]);
return new DataValidationException(errors[0]);
}
else
{
return new AggregateException(
errors.Select(x => new Exception(x)));
errors.Select(x => new DataValidationException(x)));
}
}
}

21
src/Avalonia.Base/Data/DataValidationException.cs

@ -0,0 +1,21 @@
using System;
namespace Avalonia.Data
{
/// <summary>
/// Exception, which wrap validation errors.
/// </summary>
public class DataValidationException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="DataValidationException"/> class.
/// </summary>
/// <param name="errorData">Data of validation error.</param>
public DataValidationException(object errorData) : base(errorData?.ToString())
{
ErrorData = errorData;
}
public object ErrorData { get; }
}
}

24
src/Avalonia.Controls/DataValidationErrors.cs

@ -22,8 +22,8 @@ namespace Avalonia.Controls
/// <summary>
/// Defines the DataValidationErrors.Errors attached property.
/// </summary>
public static readonly AttachedProperty<IEnumerable<Exception>> ErrorsProperty =
AvaloniaProperty.RegisterAttached<DataValidationErrors, Control, IEnumerable<Exception>>("Errors");
public static readonly AttachedProperty<IEnumerable<object>> ErrorsProperty =
AvaloniaProperty.RegisterAttached<DataValidationErrors, Control, IEnumerable<object>>("Errors");
/// <summary>
/// Defines the DataValidationErrors.HasErrors attached property.
@ -76,7 +76,7 @@ namespace Avalonia.Controls
private static void ErrorsChanged(AvaloniaPropertyChangedEventArgs e)
{
var control = (Control)e.Sender;
var errors = (IEnumerable<Exception>)e.NewValue;
var errors = (IEnumerable<object>)e.NewValue;
var hasErrors = false;
if (errors != null && errors.Any())
@ -91,11 +91,11 @@ namespace Avalonia.Controls
classes.Set(":error", (bool)e.NewValue);
}
public static IEnumerable<Exception> GetErrors(Control control)
public static IEnumerable<object> GetErrors(Control control)
{
return control.GetValue(ErrorsProperty);
}
public static void SetErrors(Control control, IEnumerable<Exception> errors)
public static void SetErrors(Control control, IEnumerable<object> errors)
{
control.SetValue(ErrorsProperty, errors);
}
@ -112,14 +112,14 @@ namespace Avalonia.Controls
return control.GetValue(HasErrorsProperty);
}
private static IEnumerable<Exception> UnpackException(Exception exception)
private static IEnumerable<object> UnpackException(Exception exception)
{
if (exception != null)
{
var aggregate = exception as AggregateException;
var exceptions = aggregate == null ?
(IEnumerable<Exception>)new[] { exception } :
aggregate.InnerExceptions;
new[] { GetExceptionData(exception) } :
aggregate.InnerExceptions.Select(GetExceptionData).ToArray();
var filtered = exceptions.Where(x => !(x is BindingChainException)).ToList();
if (filtered.Count > 0)
@ -130,5 +130,13 @@ namespace Avalonia.Controls
return null;
}
private static object GetExceptionData(Exception exception)
{
if (exception is DataValidationException dataValidationException)
return dataValidationException.ErrorData;
return exception;
}
}
}

Loading…
Cancel
Save