Skip to content

Commit 156656c

Browse files
dtwk2Declan Taylor
andauthored
Added support for GetErrors parameter being null (#69)
* Added support for GetErrors parameter being null * Update ReactiveValidationObject.cs * Added missing reference * Added underscore to field-name. Removed trailing whitespace. Made method, GetMemberInfoName, private. Co-authored-by: Declan Taylor <declan.taylor@pcs-instruments.com>
1 parent 21359ad commit 156656c

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/ReactiveUI.Validation/Helpers/ReactiveValidationObject.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.Collections;
9+
using System.Collections.Generic;
910
using System.ComponentModel;
1011
using System.Linq;
1112
using System.Reactive.Concurrency;
@@ -24,6 +25,7 @@ namespace ReactiveUI.Validation.Helpers
2425
public abstract class ReactiveValidationObject<TViewModel> : ReactiveObject, IValidatableViewModel, INotifyDataErrorInfo
2526
{
2627
private readonly ObservableAsPropertyHelper<bool> _hasErrors;
28+
private readonly Dictionary<string, string> _propertyMemberNameDictionary = new Dictionary<string, string>();
2729

2830
/// <summary>
2931
/// Initializes a new instance of the <see cref="ReactiveValidationObject{TViewModel}"/> class.
@@ -60,25 +62,38 @@ protected ReactiveValidationObject(IScheduler scheduler = null)
6062
/// <inheritdoc />
6163
public virtual IEnumerable GetErrors(string propertyName)
6264
{
63-
var memberInfoName = GetType()
64-
.GetMember(propertyName)
65-
.FirstOrDefault()?
66-
.ToString();
65+
return string.IsNullOrEmpty(propertyName) ?
66+
SelectValidations()
67+
.SelectMany(validation => validation.Text)
68+
.ToArray() :
69+
GetMemberInfoName(propertyName) is string memberInfoName && string.IsNullOrEmpty(memberInfoName) == false ?
70+
SelectValidations()
71+
.Where(validation => validation.ContainsPropertyName(memberInfoName))
72+
.SelectMany(validation => validation.Text)
73+
.ToArray() :
74+
Enumerable.Empty<string>();
75+
76+
IEnumerable<IPropertyValidationComponent<TViewModel>> SelectValidations() =>
77+
78+
ValidationContext
79+
.Validations
80+
.OfType<IPropertyValidationComponent<TViewModel>>()
81+
.Where(validation => !validation.IsValid);
82+
}
6783

68-
if (memberInfoName == null)
84+
private string GetMemberInfoName(string propertyName)
85+
{
86+
if (_propertyMemberNameDictionary.ContainsKey(propertyName) == false)
6987
{
70-
return Enumerable.Empty<string>();
88+
_propertyMemberNameDictionary.Add(propertyName, GetMemberInfoName());
7189
}
7290

73-
var relatedPropertyValidations = ValidationContext
74-
.Validations
75-
.OfType<IPropertyValidationComponent<TViewModel>>()
76-
.Where(validation => validation.ContainsPropertyName(memberInfoName));
91+
return _propertyMemberNameDictionary[propertyName];
7792

78-
return relatedPropertyValidations
79-
.Where(validation => !validation.IsValid)
80-
.SelectMany(validation => validation.Text)
81-
.ToList();
93+
string GetMemberInfoName() => GetType()
94+
.GetMember(propertyName)
95+
.FirstOrDefault()?
96+
.ToString();
8297
}
8398
}
8499
}

0 commit comments

Comments
 (0)