@@ -90,7 +90,7 @@ internal static class Execute
90
90
ImmutableArray < string > . Builder notifiedCommandNames = ImmutableArray . CreateBuilder < string > ( ) ;
91
91
ImmutableArray < AttributeInfo > . Builder forwardedAttributes = ImmutableArray . CreateBuilder < AttributeInfo > ( ) ;
92
92
bool notifyRecipients = false ;
93
- bool alsoValidateProperty = false ;
93
+ bool notifyDataErrorInfo = false ;
94
94
bool hasAnyValidationAttributes = false ;
95
95
96
96
// Track the property changing event for the property, if the type supports it
@@ -108,6 +108,12 @@ internal static class Execute
108
108
notifyRecipients = isBroadcastTargetValid ;
109
109
}
110
110
111
+ // Get the class-level [NotifyDataErrorInfo] setting, if any
112
+ if ( TryGetNotifyDataErrorInfo ( fieldSymbol , out bool isValidationTargetValid ) )
113
+ {
114
+ notifyDataErrorInfo = isValidationTargetValid ;
115
+ }
116
+
111
117
// Gather attributes info
112
118
foreach ( AttributeData attributeData in fieldSymbol . GetAttributes ( ) )
113
119
{
@@ -127,9 +133,9 @@ internal static class Execute
127
133
}
128
134
129
135
// Check whether the property should also be validated
130
- if ( TryGetIsValidatingProperty ( fieldSymbol , attributeData , builder , out bool isValidationTargetValid ) )
136
+ if ( TryGetNotifyDataErrorInfo ( fieldSymbol , attributeData , builder , out isValidationTargetValid ) )
131
137
{
132
- alsoValidateProperty = isValidationTargetValid ;
138
+ notifyDataErrorInfo = isValidationTargetValid ;
133
139
134
140
continue ;
135
141
}
@@ -171,7 +177,7 @@ internal static class Execute
171
177
}
172
178
173
179
// Log the diagnostic for missing validation attributes, if any
174
- if ( alsoValidateProperty && ! hasAnyValidationAttributes )
180
+ if ( notifyDataErrorInfo && ! hasAnyValidationAttributes )
175
181
{
176
182
builder . Add (
177
183
MissingValidationAttributesForNotifyDataErrorInfoError ,
@@ -190,7 +196,7 @@ internal static class Execute
190
196
propertyChangedNames . ToImmutable ( ) ,
191
197
notifiedCommandNames . ToImmutable ( ) ,
192
198
notifyRecipients ,
193
- alsoValidateProperty ,
199
+ notifyDataErrorInfo ,
194
200
forwardedAttributes . ToImmutable ( ) ) ;
195
201
}
196
202
@@ -482,6 +488,35 @@ private static bool TryGetIsNotifyingRecipients(
482
488
return false ;
483
489
}
484
490
491
+ /// <summary>
492
+ /// Checks whether a given generated property should also validate its value.
493
+ /// </summary>
494
+ /// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
495
+ /// <param name="isValidationTargetValid">Whether or not the the property is in a valid target that can validate values.</param>
496
+ /// <returns>Whether or not the generated property for <paramref name="fieldSymbol"/> used <c>[NotifyDataErrorInfo]</c>.</returns>
497
+ private static bool TryGetNotifyDataErrorInfo ( IFieldSymbol fieldSymbol , out bool isValidationTargetValid )
498
+ {
499
+ if ( fieldSymbol . ContainingType ? . HasOrInheritsAttributeWithFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.NotifyDataErrorInfoAttribute" ) == true )
500
+ {
501
+ // If the containing type is valid, track it
502
+ if ( fieldSymbol . ContainingType . InheritsFromFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator" ) )
503
+ {
504
+ isValidationTargetValid = true ;
505
+
506
+ return true ;
507
+ }
508
+
509
+ // Otherwise, ignore the attribute but don't emit a diagnostic (same as above)
510
+ isValidationTargetValid = false ;
511
+
512
+ return true ;
513
+ }
514
+
515
+ isValidationTargetValid = false ;
516
+
517
+ return false ;
518
+ }
519
+
485
520
/// <summary>
486
521
/// Checks whether a given generated property should also validate its value.
487
522
/// </summary>
@@ -490,7 +525,7 @@ private static bool TryGetIsNotifyingRecipients(
490
525
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
491
526
/// <param name="isValidationTargetValid">Whether or not the the property is in a valid target that can validate values.</param>
492
527
/// <returns>Whether or not the generated property for <paramref name="fieldSymbol"/> used <c>[NotifyDataErrorInfo]</c>.</returns>
493
- private static bool TryGetIsValidatingProperty (
528
+ private static bool TryGetNotifyDataErrorInfo (
494
529
IFieldSymbol fieldSymbol ,
495
530
AttributeData attributeData ,
496
531
ImmutableArray < Diagnostic > . Builder diagnostics ,
0 commit comments