-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequiredIfOtherPropertyTrueAttribute.cs
39 lines (32 loc) · 1.36 KB
/
RequiredIfOtherPropertyTrueAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.ComponentModel.DataAnnotations;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class RequiredIfOtherPropertyTrueAttribute : RequiredAttribute
{
public RequiredIfOtherPropertyTrueAttribute(string otherProperty)
{
if (otherProperty == null)
{
throw new ArgumentNullException("otherProperty");
}
OtherProperty = otherProperty;
}
public string OtherProperty { get; private set; }
public override bool RequiresValidationContext => true;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty);
if (otherPropertyInfo == null)
{
throw new MissingMemberException($"The property {OtherProperty} is missing.");
}
// https://github.com/dotnet/aspnetcore/issues/41582
// At this point all the properties of the IndexModel are not set.
// The property NameRequired is always false.
object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (!Equals(true, otherPropertyValue))
{
return null; // no validation required if the other property is not true.
}
return base.IsValid(value, validationContext);
}
}