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
{
<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">
<NewCommandTemplate>
<Button Color="Color.Success" Clicked="@context.Clicked" title="Create user">
@ -40,11 +40,35 @@ else
</Button>
</CancelCommandTemplate>
</DataGridCommandColumn>
<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.Id)" Caption="#" Sortable="false" />
<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.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">
<DisplayTemplate>
@{
@ -55,7 +79,7 @@ else
</DisplayTemplate>
<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)
{
<SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem>

View File

@ -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<User, Dictionary<string, object>> 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<User, Dictionary<string, object>> 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;
}
}
}