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

Loading…
Cancel
Save