-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidationRules.cs
30 lines (28 loc) · 1.39 KB
/
ValidationRules.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
using DevExpress.Mvvm;
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Windows.Controls;
namespace RegistrationForm.Common {
public class RequiredValidationRule : ValidationRule {
public static string GetErrorMessage(string fieldName, object fieldValue, object nullValue = null) {
string errorMessage = string.Empty;
if(nullValue != null && nullValue.Equals(fieldValue))
errorMessage = string.Format("You cannot leave the {0} field empty.", fieldName);
if(fieldValue == null || string.IsNullOrEmpty(fieldValue.ToString()))
errorMessage = string.Format("You cannot leave the {0} field empty.", fieldName);
return errorMessage;
}
public static string GetErrorMessage<T>(Expression<Func<T>> expression, object fieldValue, object nullValue = null) {
string fieldName = BindableBase.GetPropertyName(expression);
return GetErrorMessage(fieldName, fieldValue, nullValue);
}
public string FieldName { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
string error = GetErrorMessage(FieldName, value);
if(!string.IsNullOrEmpty(error))
return new ValidationResult(false, error);
return ValidationResult.ValidResult;
}
}
}