Skip to content

Commit 488a762

Browse files
committed
Add AlsoValidatePropertyAttribute type
1 parent e202a55 commit 488a762

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
namespace CommunityToolkit.Mvvm.ComponentModel;
8+
9+
/// <summary>
10+
/// An attribute that can be used to support <see cref="ObservablePropertyAttribute"/> in generated properties, when applied to
11+
/// fields contained in a type that is inheriting from <see cref="ObservableValidator"/> and using any validation attributes.
12+
/// When this attribute is used, the generated property setter will also call <see cref="ObservableValidator.ValidateProperty(object?, string)"/>.
13+
/// This allows generated properties to opt-in into validation behavior without having to fallback into a full explicit observable property.
14+
/// <para>
15+
/// This attribute can be used as follows:
16+
/// <code>
17+
/// partial class MyViewModel : ObservableValidator
18+
/// {
19+
/// [ObservableProperty]
20+
/// [AlsoValidateProperty]
21+
/// [Required]
22+
/// [MinLength(2)]
23+
/// private string username;
24+
/// }
25+
/// </code>
26+
/// </para>
27+
/// And with this, code analogous to this will be generated:
28+
/// <code>
29+
/// partial class MyViewModel
30+
/// {
31+
/// [Required]
32+
/// [MinLength(2)]
33+
/// public string Username
34+
/// {
35+
/// get => username;
36+
/// set => SetProperty(ref username, value, validate: true);
37+
/// }
38+
/// }
39+
/// </code>
40+
/// </summary>
41+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
42+
public sealed class AlsoValidatePropertyAttribute : Attribute
43+
{
44+
}

0 commit comments

Comments
 (0)