This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4803897
commit db0ba56
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/ControlGallery/ControlGallery/Models/LogInModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ControlGallery.Models | ||
{ | ||
public class LogInModel | ||
{ | ||
[Required, EmailAddress] | ||
public string Email { get; set; } | ||
|
||
[Required, MinLength(8), MaxLength(16)] | ||
public string Password { get; set; } | ||
|
||
[Required, MinLength(8), MaxLength(16)] | ||
public string ConfirmPassword { get; set; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
samples/ControlGallery/ControlGallery/Models/LogInModelValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using FluentValidation; | ||
|
||
namespace ControlGallery.Models | ||
{ | ||
public class LogInModelValidator : AbstractValidator<LogInModel> | ||
{ | ||
public LogInModelValidator() | ||
{ | ||
RuleFor(m => m.Email) | ||
.NotNull() | ||
.EmailAddress(); | ||
|
||
RuleFor(m => m.Password) | ||
.NotNull() | ||
.Length(8, 16); | ||
|
||
RuleFor(m => m.ConfirmPassword) | ||
.Cascade(CascadeMode.StopOnFirstFailure) | ||
.NotNull() | ||
.Equal(m => m.Password) | ||
.WithMessage("'{PropertyName}' should be equal to '{ComparisonProperty}'"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
samples/ControlGallery/ControlGallery/Views/Validations.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.MobileBlazorBindings.Forms | ||
@using ControlGallery.Models | ||
|
||
@page "/validations" | ||
|
||
<ContentView> | ||
<StackLayout> | ||
<Button Text="Add FluentValidator" OnClick="AddFluentValidator" IsEnabled="!useFluentValidator" /> | ||
<Button Text="Add DataAnnotationsValidator" OnClick="AddDataAnnotationsValidator" IsEnabled="!useDataAnnotationsValidator" /> | ||
|
||
<CascadingEditContext Model="LogInModel"> | ||
@if (useFluentValidator) | ||
{ | ||
<FluentValidator Validator="new LogInModelValidator()" /> | ||
} | ||
@if (useDataAnnotationsValidator) | ||
{ | ||
<DataAnnotationsValidator /> | ||
} | ||
|
||
<ValidatedEntry @bind-Text="@LogInModel.Email" Placeholder="Email" /> | ||
<ValidationLabel For="@(() => LogInModel.Email)" TextColor="Color.Red" /> | ||
|
||
<ValidatedEntry @bind-Text="@LogInModel.Password" Placeholder="Password" /> | ||
<ValidationLabel For="@(() => LogInModel.Password)" TextColor="Color.Red" /> | ||
|
||
<ValidatedEntry @bind-Text="@LogInModel.ConfirmPassword" Placeholder="Confirm password" /> | ||
<ValidationLabel For="@(() => LogInModel.ConfirmPassword)" TextColor="Color.Red" /> | ||
|
||
<SubmitButton Text="Submit" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit" /> | ||
<Label Text="@statusText" /> | ||
</CascadingEditContext> | ||
</StackLayout> | ||
</ContentView> | ||
|
||
@code { | ||
LogInModel LogInModel { get; } = new LogInModel(); | ||
bool useFluentValidator = false; | ||
bool useDataAnnotationsValidator = false; | ||
string statusText; | ||
|
||
void OnValidSubmit() => statusText = "Success!"; | ||
void OnInvalidSubmit() => statusText = "Failure!"; | ||
|
||
void AddFluentValidator() => useFluentValidator = true; | ||
void AddDataAnnotationsValidator() => useDataAnnotationsValidator = true; | ||
} |