-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidationRules.vb
32 lines (26 loc) · 1.59 KB
/
ValidationRules.vb
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
Imports DevExpress.Mvvm
Imports System
Imports System.Globalization
Imports System.Linq.Expressions
Imports System.Windows.Controls
Namespace RegistrationForm.Common
Public Class RequiredValidationRule
Inherits ValidationRule
Public Shared Function GetErrorMessage(ByVal fieldName As String, ByVal fieldValue As Object, ByVal Optional nullValue As Object = Nothing) As String
Dim errorMessage As String = String.Empty
If nullValue IsNot Nothing AndAlso nullValue.Equals(fieldValue) Then errorMessage = String.Format("You cannot leave the {0} field empty.", fieldName)
If fieldValue Is Nothing OrElse String.IsNullOrEmpty(fieldValue.ToString()) Then errorMessage = String.Format("You cannot leave the {0} field empty.", fieldName)
Return errorMessage
End Function
Public Shared Function GetErrorMessage(Of T)(ByVal expression As Expression(Of Func(Of T)), ByVal fieldValue As Object, ByVal Optional nullValue As Object = Nothing) As String
Dim fieldName As String = BindableBase.GetPropertyName(expression)
Return GetErrorMessage(fieldName, fieldValue, nullValue)
End Function
Public Property FieldName As String
Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As CultureInfo) As ValidationResult
Dim [error] As String = GetErrorMessage(FieldName, value)
If Not String.IsNullOrEmpty([error]) Then Return New ValidationResult(False, [error])
Return ValidationResult.ValidResult
End Function
End Class
End Namespace