-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidLogonAttribute.cs
39 lines (28 loc) · 1.15 KB
/
ValidLogonAttribute.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;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WebUi.CustomValidatorAttributes
{
public class ValidLogonAttribute : ValidationAttribute
{
private string _comparisonProperty;
public ValidLogonAttribute(string comparisonProperty)
{
_comparisonProperty = comparisonProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ErrorMessage = ErrorMessageString;
var logon = (string)value;
var property = validationContext.ObjectType.GetProperty(_comparisonProperty);
if (property == null)
throw new ArgumentException("Property [" + _comparisonProperty + "] was not found");
var preferredName = (string)property.GetValue(validationContext.ObjectInstance);
if (logon != null && logon == preferredName)
return new ValidationResult("Logon cannot be the same as the preferred name.");
return ValidationResult.Success;
}
}
}