From 697886730ebaa5a3b9778dfc53b6fcb8114f532b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Sun, 16 Aug 2020 22:10:23 +0200 Subject: [PATCH] added validator --- UserService.db | Bin 40960 -> 40960 bytes UserService/Pages/Users.razor | 34 ++++++++++++++++++++---- UserService/Pages/Users.razor.cs | 44 ++++++++++++++++++++++++------- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/UserService.db b/UserService.db index cef1d65e32c2db506dde6510f5271a1def64ea62..11403be6291cbe783e4caabbba18c425a201ee63 100644 GIT binary patch delta 145 zcmZoTz|?SnX#6O#lD@ delta 146 zcmZoTz|?SnX# + - - + + + + + + + + + Please enter a valid common name! + + + + + - + + + + + + Please enter the email. + Email is ok. + Enter valid email! + + + + + @{ @@ -55,7 +79,7 @@ else - @foreach (var item in OrganizationUnits) { @item.CommonName diff --git a/UserService/Pages/Users.razor.cs b/UserService/Pages/Users.razor.cs index a134c6f..ff80935 100644 --- a/UserService/Pages/Users.razor.cs +++ b/UserService/Pages/Users.razor.cs @@ -1,8 +1,10 @@ using System; using Microsoft.AspNetCore.Components; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; +using Blazorise; using Blazorise.DataGrid; using Microsoft.JSInterop; using UserService.DatabaseLayer.DataModels; @@ -47,15 +49,39 @@ namespace UserService.Pages arg.Cancel = true; } - - protected async Task RowUpdatingCallback(CancellableRowChange> arg) - { - if (arg == null) throw new ArgumentNullException(nameof(arg)); - var user = arg.Item; - user.Parent = OrganizationUnits.FirstOrDefault(x => x.Id == (int?)arg.Values[nameof(Node.ParentId)]); - var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false); - arg.Cancel = !result; - } + protected static void ValidateCommonName(ValidatorEventArgs e) + { + if (e == null) throw new ArgumentNullException(nameof(e)); + var commonName = e.Value?.ToString(); + + if (string.IsNullOrEmpty(commonName)) + e.Status = ValidationStatus.Error; + else + e.Status = ValidationStatus.Success; + } + + protected static void ValidateEmail(ValidatorEventArgs e) + { + if (e == null) throw new ArgumentNullException(nameof(e)); + var email = e.Value?.ToString(); + + if (string.IsNullOrEmpty(email)) + e.Status = ValidationStatus.None; + else if (!new EmailAddressAttribute().IsValid(email)) + { + e.Status = ValidationStatus.Error; + } + else + e.Status = ValidationStatus.Success; + } + protected async Task RowUpdatingCallback(CancellableRowChange> arg) + { + if (arg == null) throw new ArgumentNullException(nameof(arg)); + var user = arg.Item; + user.Parent = OrganizationUnits.FirstOrDefault(x => x.Id == (int?) arg.Values[nameof(Node.ParentId)]); + var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false); + arg.Cancel = !result; + } } } \ No newline at end of file