generated from nventive/Template
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCreateAccountFormViewModel.cs
More file actions
118 lines (100 loc) · 2.7 KB
/
CreateAccountFormViewModel.cs
File metadata and controls
118 lines (100 loc) · 2.7 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using Chinook.DynamicMvvm;
using FluentValidation;
using Microsoft.Extensions.Localization;
using Uno.Extensions;
namespace ApplicationTemplate.Presentation;
public sealed class CreateAccountFormViewModel : ViewModel
{
public CreateAccountFormViewModel()
{
this.AddValidation(this.GetProperty(x => x.DateOfBirth));
// Restore validation when this issue is resolved: https://github.com/nventive/Nventive.View/issues/41
// this.AddValidation(this.GetProperty(x => x.FavoriteDadNames));
this.AddValidation(this.GetProperty(x => x.AgreeToTermsOfServices));
}
public string FirstName
{
get => this.Get<string>();
set => this.Set(value);
}
public string LastName
{
get => this.Get<string>();
set => this.Set(value);
}
public string Email
{
get => this.Get<string>();
set => this.Set(value);
}
public string PhoneNumber
{
get => this.Get<string>();
set => this.Set(value);
}
public string PostalCode
{
get => this.Get<string>();
set => this.Set(value);
}
public DateTimeOffset DateOfBirth
{
get => this.Get<DateTimeOffset>(initialValue: DateTimeOffset.Now);
set => this.Set(value);
}
public object[] FavoriteDadNames
{
get => this.Get(initialValue: Array.Empty<object>());
set => this.Set(value);
}
public bool AgreeToTermsOfServices
{
get => this.Get<bool>();
set => this.Set(value);
}
public IDynamicCommand ValidateProperty => this.GetCommandFromTask<string>(async (ct, propertyName) =>
{
await this.ValidateProperty(ct, this.GetProperty(propertyName));
});
}
public class CreateAccountFormValidator : AbstractValidator<CreateAccountFormViewModel>
{
public CreateAccountFormValidator(IStringLocalizer localizer)
{
RuleFor(x => x.FirstName).NotEmpty();
RuleFor(x => x.LastName).NotEmpty();
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage(_ => localizer["ValidationNotEmpty_Email"])
.EmailAddress()
.WithMessage(_ => localizer["ValidationError_Email"]);
RuleFor(x => x.PhoneNumber)
.NotEmpty()
.MustBePhoneNumber()
.WithMessage(localizer["CreateAccount_PhoneNumberValidation"]);
RuleFor(x => x.PostalCode)
.NotEmpty()
.Length(7)
.WithMessage(localizer["CreateAccount_PostalValidation"]); // "A1A 1A1".Length = 7
RuleFor(x => x.DateOfBirth)
.NotEmpty()
.MustBe18OrOlder()
.WithMessage(localizer["CreateAccount_DateOfBirthValidation"]);
/*
RuleFor(x => x.FavoriteDadNames)
.Must(favDadNames =>
{
if (favDadNames == null)
{
return false;
}
return favDadNames.Length >= 1;
})
.WithMessage(localizer["CreateAccount_FavoriteDadNameValidation"]);
*/
RuleFor(x => x.AgreeToTermsOfServices)
.Equal(true)
.WithMessage(localizer["CreateAccount_TermsOfServiceValidation"]);
}
}