-
Notifications
You must be signed in to change notification settings - Fork 0
ComplexScenerios
Sebastian Cygan edited this page Nov 8, 2020
·
2 revisions
content in progress
content in progress
This is a pretty often scenario when there are properties in your data models which each of them need the same validation pattern. In such a situation, you do not need to repeat yourself with the same condition chain for each of these properties. It is very simple to build the condition chain once and then to use it every time it should be used.
You can do this by creating an extension for the FvConditionRule class. It is possible in such languages like C# and VB.Net and there is some example below.
'For creating extension method you need to use this namespace
Imports System.Runtime.CompilerServices
'These namespaces are needed like in any other code using the Forvalidate
Imports Fortedo.ForValidate.Extensions
Imports Fortedo.ForValidate
Namespace Validators
'Extensions in VB.Net can be created only in a module (static class in C#)
Module ValidatorExtensions
'Always the same pattern for creating an extension method
<Extension()> _
Public Function IsUserName(ByVal rule As FvConditionRule) As FvConditionRule
'Specify your conditions as you would do for some AddRule method in any validator
'But you use the rule parameter instead of AddRule method
'Remember to return the entire specified condition chain
Return rule _
.Length(1, 20) _
.Matches("^[a-z]*\.[a-z]*[0-9]{0,1}$") _
End Function
End Module
End NamespaceWhen the extension is defined you can use it like any other conditions. You can also mix your own conditions with the built-in ones.
Imports Fortedo.ForValidate.Extensions
Imports Fortedo.ForValidate
Namespace Validators
Public Class RequestValidator
Inherits FvValidatorBase
Public Sub New()
AddRule("Title") _
.Length(1, 100)
AddRule("CreateUser") _
.IsUserName
AddRule("AcceptUser") _
.IsUserName
AddRule("CloseUser") _
.IsUserName _
.NotEqual("main.admin")
End Sub
End Class
End Namespace