Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Add validations playground sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Nov 23, 2020
1 parent 4803897 commit db0ba56
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions samples/ControlGallery/ControlGallery/ControlGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Accelist.FluentValidation.Blazor" Version="3.0.0" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="2.80.1" />
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.MobileBlazorBindings.Forms\Microsoft.MobileBlazorBindings.Forms.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.MobileBlazorBindings.SkiaSharp\Microsoft.MobileBlazorBindings.SkiaSharp.csproj" />
<!--
NOTE: This project doesn't currently use any DualScreen features, but this reference is here
Expand Down
16 changes: 16 additions & 0 deletions samples/ControlGallery/ControlGallery/Models/LogInModel.cs
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; }
}
}
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}'");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Button Text="Navigation playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/navigation"))"></Button>
<Button Text="Gesture playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/gestureplayground"))"></Button>
<Button Text="Skia Playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/skia"))"></Button>
<Button Text="Validations Playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/validations"))"></Button>
</StackLayout>
</ScrollView>
</ContentPage>
48 changes: 48 additions & 0 deletions samples/ControlGallery/ControlGallery/Views/Validations.razor
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;
}

0 comments on commit db0ba56

Please sign in to comment.