-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputValidatorPageViewModel.cs
69 lines (58 loc) · 2.32 KB
/
InputValidatorPageViewModel.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace MADE.Samples.Features.Samples.ViewModels
{
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.Messaging;
using FluentValidation;
using MADE.Data.Validation;
using MADE.Data.Validation.Validators;
using MADE.UI.Views.Navigation;
using MADE.UI.Views.Navigation.ViewModels;
public class InputValidatorPageViewModel : PageViewModel
{
private string inputText;
private DateTimeOffset? inputDate;
private string fluentValidationInputText;
public InputValidatorPageViewModel(INavigationService navigationService, IMessenger messenger)
: base(navigationService, messenger)
{
}
public ValidatorCollection InputTextValidators { get; } =
new ValidatorCollection { new RequiredValidator(), new MaxLengthValidator(16) };
public string InputText
{
get => inputText;
set => this.SetProperty(ref inputText, value);
}
public ValidatorCollection InputDateValidators { get; } = new ValidatorCollection
{
new RequiredValidator(),
new BetweenValidator(DateTimeOffset.Now.AddDays(-7), DateTimeOffset.Now.AddDays(7))
};
public DateTimeOffset? InputDate
{
get => inputDate;
set => this.SetProperty(ref inputDate, value);
}
public FluentValidatorCollection<string> FluentValidationValidators { get; } = new(GetFluentValidators());
public string FluentValidationInputText
{
get => fluentValidationInputText;
set => this.SetProperty(ref fluentValidationInputText, value);
}
private static IEnumerable<IValidator<string>> GetFluentValidators()
{
var requiredValidator = new InlineValidator<string>();
requiredValidator
.RuleFor(x => x)
.NotEmpty()
.WithMessage("A value is required.");
var maxLengthValidator = new InlineValidator<string>();
maxLengthValidator
.RuleFor(x => x.Length)
.LessThanOrEqualTo(16)
.WithMessage("The length of the value must be less than or equal to 16 characters.");
return new[] { requiredValidator, maxLengthValidator };
}
}
}