17
17
using ReactiveUI . Validation . Collections ;
18
18
using ReactiveUI . Validation . Components . Abstractions ;
19
19
using ReactiveUI . Validation . Contexts ;
20
+ using ReactiveUI . Validation . Formatters ;
21
+ using ReactiveUI . Validation . Formatters . Abstractions ;
20
22
using ReactiveUI . Validation . States ;
23
+ using Splat ;
21
24
22
25
namespace ReactiveUI . Validation . Helpers
23
26
{
@@ -34,9 +37,18 @@ public abstract class ReactiveValidationObject<TViewModel> : ReactiveValidationO
34
37
/// <summary>
35
38
/// Initializes a new instance of the <see cref="ReactiveValidationObject{TViewModel}"/> class.
36
39
/// </summary>
37
- /// <param name="scheduler">Scheduler for OAPHs and for the the ValidationContext.</param>
38
- protected ReactiveValidationObject ( IScheduler ? scheduler = null )
39
- : base ( scheduler )
40
+ /// <param name="scheduler">
41
+ /// Scheduler for the <see cref="ValidationContext"/>. Uses <see cref="CurrentThreadScheduler"/> by default.
42
+ /// </param>
43
+ /// <param name="formatter">
44
+ /// Validation formatter. Defaults to <see cref="SingleLineFormatter"/>. In order to override the global
45
+ /// default value, implement <see cref="IValidationTextFormatter{TOut}"/> and register an instance of
46
+ /// IValidationTextFormatter<string> into Splat.Locator.
47
+ /// </param>
48
+ protected ReactiveValidationObject (
49
+ IScheduler ? scheduler = null ,
50
+ IValidationTextFormatter < string > ? formatter = null )
51
+ : base ( scheduler , formatter )
40
52
{
41
53
}
42
54
}
@@ -47,14 +59,28 @@ protected ReactiveValidationObject(IScheduler? scheduler = null)
47
59
public abstract class ReactiveValidationObject : ReactiveObject , IValidatableViewModel , INotifyDataErrorInfo
48
60
{
49
61
private readonly HashSet < string > _mentionedPropertyNames = new HashSet < string > ( ) ;
62
+ private readonly IValidationTextFormatter < string > _formatter ;
50
63
private bool _hasErrors ;
51
64
52
65
/// <summary>
53
66
/// Initializes a new instance of the <see cref="ReactiveValidationObject"/> class.
54
67
/// </summary>
55
- /// <param name="scheduler">Scheduler for OAPHs and for the the ValidationContext.</param>
56
- protected ReactiveValidationObject ( IScheduler ? scheduler = null )
68
+ /// <param name="scheduler">
69
+ /// Scheduler for the <see cref="ValidationContext"/>. Uses <see cref="CurrentThreadScheduler"/> by default.
70
+ /// </param>
71
+ /// <param name="formatter">
72
+ /// Validation formatter. Defaults to <see cref="SingleLineFormatter"/>. In order to override the global
73
+ /// default value, implement <see cref="IValidationTextFormatter{TOut}"/> and register an instance of
74
+ /// IValidationTextFormatter<string> into Splat.Locator.
75
+ /// </param>
76
+ protected ReactiveValidationObject (
77
+ IScheduler ? scheduler = null ,
78
+ IValidationTextFormatter < string > ? formatter = null )
57
79
{
80
+ _formatter = formatter ??
81
+ Locator . Current . GetService < IValidationTextFormatter < string > > ( ) ??
82
+ SingleLineFormatter . Default ;
83
+
58
84
ValidationContext = new ValidationContext ( scheduler ) ;
59
85
ValidationContext . Validations
60
86
. ToObservableChangeSet ( )
@@ -89,13 +115,13 @@ public bool HasErrors
89
115
/// <returns>A list of error messages, usually strings.</returns>
90
116
/// <inheritdoc />
91
117
public virtual IEnumerable GetErrors ( string propertyName ) =>
92
- string . IsNullOrEmpty ( propertyName ) ?
93
- SelectInvalidPropertyValidations ( )
94
- . SelectMany ( validation => validation . Text ?? ValidationText . None )
95
- . ToArray ( ) :
96
- SelectInvalidPropertyValidations ( )
118
+ string . IsNullOrEmpty ( propertyName )
119
+ ? SelectInvalidPropertyValidations ( )
120
+ . Select ( state => _formatter . Format ( state . Text ?? ValidationText . None ) )
121
+ . ToArray ( )
122
+ : SelectInvalidPropertyValidations ( )
97
123
. Where ( validation => validation . ContainsPropertyName ( propertyName ) )
98
- . SelectMany ( validation => validation . Text ?? ValidationText . None )
124
+ . Select ( state => _formatter . Format ( state . Text ?? ValidationText . None ) )
99
125
. ToArray ( ) ;
100
126
101
127
/// <summary>
0 commit comments