-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithoutSubmit.razor
94 lines (76 loc) · 2.54 KB
/
WithoutSubmit.razor
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
@page "/withoutsubmit"
@using Blazored.FluentValidation
@using FluentValidation
<FluentStack>
<h3>Without Submit</h3>
<FluentAnchor Href="https://github.com/yamanaiyuki/FluentTextArea1085/blob/main/Pages/WithoutSubmit.razor" IconStart="@(new Icons.Regular.Size20.Document())">Source</FluentAnchor>
</FluentStack>
<EditForm EditContext="editContext">
<FluentValidationValidator />
<FluentGrid style="width:500px;">
<FluentGridItem xs="12" sm="6">
<FluentTextField Label="Subject" @bind-Value=item.Subject />
</FluentGridItem>
<FluentGridItem xs="12" sm="6">
<ValidationMessage For="() => item.Subject" />
</FluentGridItem>
<FluentGridItem xs="12" sm="6">
<FluentTextArea Label="Message" Resize="TextAreaResize.Both" @bind-Value=item.Message />
</FluentGridItem>
<FluentGridItem xs="12" sm="6">
<ValidationMessage For="() => item.Message" />
</FluentGridItem>
</FluentGrid>
<br />
<FluentButton Appearance="Appearance.Accent" OnClick="HandleSubmit">Send</FluentButton>
</EditForm>
<br />
@if (templates.Count > 0)
{
<FluentDataGrid Items="templates.AsQueryable()" style="width:500px;">
<PropertyColumn Property="x => x.DateTime" Format="yyyy-MM-dd HH:mm:ss" />
<PropertyColumn Property="x => x.Subject" />
<PropertyColumn Property="x => x.Message" Class="multiline-text" />
</FluentDataGrid>
}
@code {
readonly List<MailTemplate> templates = new();
EditContext? editContext;
MailTemplate item = new();
protected override void OnInitialized()
{
editContext = new(item);
}
void HandleSubmit()
{
if (editContext != null && editContext.Validate())
{
ValidSubmit();
}
}
void ValidSubmit()
{
item.DateTime = DateTime.Now;
templates.Add(item);
item = new()
{
Subject = "Re: " + templates[^1].Subject,
Message = "anonymous wrote:\n>" + templates[^1].Message.Replace("\n", "\n> "),
};
editContext = new(item);
}
public class MailTemplate
{
public DateTime DateTime { get; set; }
public string Subject { get; set; } = "";
public string Message { get; set; } = "";
}
public class MailTemplateValidator : AbstractValidator<MailTemplate>
{
public MailTemplateValidator()
{
RuleFor(x => x.Subject).NotEmpty();
RuleFor(x => x.Message).NotEmpty();
}
}
}