added validator

This commit is contained in:
Holger Börchers 2020-08-16 22:10:23 +02:00
parent ccb5b6a154
commit 697886730e
3 changed files with 64 additions and 14 deletions

Binary file not shown.

View File

@ -12,7 +12,7 @@
} }
else else
{ {
<DataGrid TItem="User" RowSelectable="@(u => false)" Editable="true" EditMode="DataGridEditMode.Inline" RowRemoving="RowDeletingCallback" Data="@Users" RowInserted="RowInsertedCallback" RowUpdating="RowUpdatingCallback"> <DataGrid TItem="User" RowSelectable="@(u => false)" Editable="true" EditMode="DataGridEditMode.Popup" RowRemoving="RowDeletingCallback" Data="@Users" RowInserted="RowInsertedCallback" RowUpdating="RowUpdatingCallback">
<DataGridCommandColumn TItem="User"> <DataGridCommandColumn TItem="User">
<NewCommandTemplate> <NewCommandTemplate>
<Button Color="Color.Success" Clicked="@context.Clicked" title="Create user"> <Button Color="Color.Success" Clicked="@context.Clicked" title="Create user">
@ -41,10 +41,34 @@ else
</CancelCommandTemplate> </CancelCommandTemplate>
</DataGridCommandColumn> </DataGridCommandColumn>
<DataGridColumn TItem="User" Field="@nameof(User.Id)" Caption="#" Sortable="false" /> <DataGridColumn TItem="User" Field="@nameof(User.Id)" Caption="#" Sortable="false" />
<DataGridColumn TItem="User" Field="@nameof(User.CommonName)" Caption="CN" CellsEditableOnEditCommand="false" Editable="true"/>
<DataGridColumn TItem="User" Field="@nameof(User.CommonName)" Caption="CN" CellsEditableOnEditCommand="false" Editable="true">
<EditTemplate>
<Validation Validator="@ValidateCommonName">
<TextEdit Placeholder="Enter common name" Text="@((string)(((CellEditContext)context).CellValue))" TextChanged="@(v=>((CellEditContext)context).CellValue=v)">
<Feedback>
<ValidationSuccess></ValidationSuccess>
<ValidationError>Please enter a valid common name!</ValidationError>
</Feedback>
</TextEdit>
</Validation>
</EditTemplate>
</DataGridColumn>
<DataGridColumn TItem="User" Field="@nameof(User.FirstName)" Caption="First Name" Editable="true"/> <DataGridColumn TItem="User" Field="@nameof(User.FirstName)" Caption="First Name" Editable="true"/>
<DataGridColumn TItem="User" Field="@nameof(User.LastName)" Caption="Last Name" Editable="true"/> <DataGridColumn TItem="User" Field="@nameof(User.LastName)" Caption="Last Name" Editable="true"/>
<DataGridColumn TItem="User" Field="@nameof(User.EMail)" Caption="EMail" Editable="true"/> <DataGridColumn TItem="User" Field="@nameof(User.EMail)" Caption="EMail" Editable="true">
<EditTemplate>
<Validation Validator="@ValidateEmail">
<TextEdit Placeholder="Enter email" Text="@((string)(((CellEditContext)context).CellValue))" TextChanged="@(v=>((CellEditContext)context).CellValue=v)">
<Feedback>
<ValidationNone>Please enter the email.</ValidationNone>
<ValidationSuccess>Email is ok.</ValidationSuccess>
<ValidationError>Enter valid email!</ValidationError>
</Feedback>
</TextEdit>
</Validation>
</EditTemplate>
</DataGridColumn>
<DataGridSelectColumn TItem="User" Field="@nameof(User.ParentId)" Caption="Parent" Editable="true"> <DataGridSelectColumn TItem="User" Field="@nameof(User.ParentId)" Caption="Parent" Editable="true">
<DisplayTemplate> <DisplayTemplate>
@{ @{
@ -55,7 +79,7 @@ else
</DisplayTemplate> </DisplayTemplate>
<EditTemplate> <EditTemplate>
<Select TValue="int" SelectedValue="@((int)(context.CellValue))" SelectedValueChanged="@(v => context.CellValue = v)" > <Select TValue="int?" SelectedValue="@((int?)(context.CellValue))" SelectedValueChanged="@(v => context.CellValue = v)" >
@foreach (var item in OrganizationUnits) @foreach (var item in OrganizationUnits)
{ {
<SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem> <SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem>

View File

@ -1,8 +1,10 @@
using System; using System;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Blazorise;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using UserService.DatabaseLayer.DataModels; using UserService.DatabaseLayer.DataModels;
@ -48,6 +50,31 @@ namespace UserService.Pages
arg.Cancel = true; arg.Cancel = true;
} }
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<User, Dictionary<string, object>> arg) protected async Task RowUpdatingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
{ {
if (arg == null) throw new ArgumentNullException(nameof(arg)); if (arg == null) throw new ArgumentNullException(nameof(arg));
@ -56,6 +83,5 @@ namespace UserService.Pages
var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false); var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false);
arg.Cancel = !result; arg.Cancel = !result;
} }
} }
} }