Column Validation in Blazor DataGrid Component | Syncfusion
@page "/configurations/bpcodes"
@using System.Collections;
@using ODLimitManager.Enums;
@using ODLimitManager.Pages.Components;
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.InPlaceEditor;
@using Syncfusion.Blazor.Inputs;
@using Syncfusion.Blazor.Data
@using ODLimitManager.Shared;
@using ODLimitManagerShared.DTOs.Configuration;
@using System.Collections.Generic;
@using System.ComponentModel.DataAnnotations;
@using System.Text.RegularExpressions;
@using System.Linq;
@inject HttpClient Http
@inject NavigationManager uriHelper;
@{
ValidatorTemplateContext txt = context as ValidatorTemplateContext;
}
@code {
ToastComponent child;
SfGrid
int level;
string? TableName;
IEnumerable
ODLimitManager.Shared.ConfigLayout configLayout;
public IEditorSettings LevelEditParams = new NumericEditCellParams { Params = new NumericTextBoxModel<object>() { ShowClearButton = true, ShowSpinButton = false } };
protected override async Task OnInitializedAsync()
{
tableNameList = await Http.GetFromJsonAsync
string tableName = tableNameList.Where(x => x.TableType == "BPCode").Select(x => x.TableName).FirstOrDefault();
TableName = tableName;
}
private async void ValueChangeHandler(String args)
{
var tableName = tableNameList.Where(x => x.TableType == "BPCode").FirstOrDefault();
if (tableName != null)
{
tableName.TableName = args;
}
TableName = args;
await Http.PutAsJsonAsync
uriHelper.NavigateTo(uriHelper.Uri, forceLoad: true);
}
public void ActionFailure(Syncfusion.Blazor.Grids.FailureEventArgs args)
{
if (args.Error == null)
return;
child.ShowToast(ToastMessageState.Error);
}
public void ActionCompletes(Syncfusion.Blazor.Grids.ActionEventArgs
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add))
{
var count = grid.TotalItemCount;
args.Data.Level = count + 1;
}
}
public void ActionBegins(Syncfusion.Blazor.Grids.ActionEventArgs
{
level = grid.TotalItemCount;
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add))
{
if (grid.TotalItemCount > 0)
{
var rt1 = (BPCodeDTO)grid.CurrentViewData.Last();
args.Data.RangeFrom = rt1.RangeTo + 1;
args.Data.RangeTo = args.Data.RangeFrom + 99;
}
else
{
args.Data.RangeFrom = 0;
args.Data.RangeTo = 100;
}
args.Data.Level = level + 1;
}
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
{
if (grid.TotalItemCount > 0)
{
if (args.Data.Level == 1)
{
var nextIndex = (BPCodeDTO)grid.CurrentViewData.ToList()[(int)args.RowIndex + 1];
if (args.Data.RangeTo > nextIndex.RangeFrom)
{
args.Cancel = true;
}
}
if (args.Data.Level > 1)
{
if (args.Data.Level == grid.TotalItemCount)
{
var prevIndex = (BPCodeDTO)grid.CurrentViewData.ToList()[(int)args.RowIndex - 1];
if (args.Data.RangeFrom < prevIndex.RangeTo)
{
args.Cancel = true;
}
}
if (args.Data.Level < grid.TotalItemCount)
{
var prevIndex = (BPCodeDTO)grid.CurrentViewData.ToList()[(int)args.RowIndex - 1];
var nextIndex = (BPCodeDTO)grid.CurrentViewData.ToList()[(int)args.RowIndex + 1];
if (args.Data.RangeFrom < prevIndex.RangeTo || args.Data.RangeTo > nextIndex.RangeFrom)
{
args.Cancel = true;
}
}
}
}
}
}
}
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using ODLimitManagerShared.DTOs.Configuration;
using Syncfusion.Blazor.Grids;
namespace ODLimitManager.Pages.Configuration.BPCodePage
{
public class Validator : ComponentBase
{
[Parameter]
public ValidatorTemplateContext context { get; set; }
private ValidationMessageStore messageStore;
[CascadingParameter]
private EditContext CurrentEditContext { get; set; }
protected override void OnInitialized()
{
messageStore = new ValidationMessageStore(CurrentEditContext);
CurrentEditContext.OnValidationRequested += ValidateRequested;
CurrentEditContext.OnFieldChanged += ValidateField;
}
private void ValidateRequested(object editContext, ValidationRequestedEventArgs validationEventArgs)
{
HandleValidation(CurrentEditContext.Field("Field"));
}
protected void ValidateField(object editContext, FieldChangedEventArgs fieldChangedEventArgs)
{
HandleValidation(fieldChangedEventArgs.FieldIdentifier);
}
protected void HandleValidation(FieldIdentifier identifier)
{
if (identifier.FieldName.Equals("RangeFrom"))
{
messageStore.Clear(identifier);
if ((context.Data as BPCodeDTO).RangeFrom > (context.Data as BPCodeDTO).RangeTo || (context.Data as BPCodeDTO).RangeTo == (context.Data as BPCodeDTO).RangeFrom)
{
messageStore.Add(identifier, "Field must be less than RangeTo");
context.ShowValidationMessage("RangeFrom", false, "value should be less than RangeTo");
}else if ((context.Data as BPCodeDTO).RangeFrom < 0)
{
messageStore.Add(identifier, "Field must be greater than 0");
context.ShowValidationMessage("RangeFrom", false, "value should be greater than 0");
}
else
{
messageStore.Clear(identifier);
context.ShowValidationMessage("RangeFrom", true, null);
}
}
if (identifier.FieldName.Equals("RangeTo"))
{
messageStore.Clear(identifier);
if ((context.Data as BPCodeDTO).RangeTo < (context.Data as BPCodeDTO).RangeFrom || (context.Data as BPCodeDTO).RangeFrom == (context.Data as BPCodeDTO).RangeTo)
{
messageStore.Add(identifier, "Field must be greater than RangeFrom");
context.ShowValidationMessage("RangeTo", false, "value should be greater than RangeFrom");
}
else if ((context.Data as BPCodeDTO).RangeTo < 0)
{
messageStore.Add(identifier, "Field must be greater than 0");
context.ShowValidationMessage("RangeTo", false, "value should be greater than 0");
}
else
{
messageStore.Clear(identifier);
context.ShowValidationMessage("RangeTo", true, null);
}
}
if (identifier.FieldName.Equals("Amount"))
{
messageStore.Clear(identifier);
if ((context.Data as BPCodeDTO).Amount < 0)
{
messageStore.Add(identifier, "Amount must be greater than 0");
context.ShowValidationMessage("Amount", false, "Amount must be greater than 0");
}
else
{
messageStore.Clear(identifier);
context.ShowValidationMessage("Amount", true, null);
}
}
}
protected void Test(EditForm editForm)
{
}
}
}
Hi Jose,
Greetings from Syncfusion support.
We are currently Validating the reported query at our end, and we will update the further details shortly. Until then we appreciate your patience.
Regards,
Sarvesh
Hi Jose,
Sorry for the delay and inconvenience caused.
From your query, we prepared sample based on your requirement but we’re unable
to reproduce the reported issue at our end. Kindly reproduce the reported issue
in the provided sample or share simple issue reproducible runnable sample to
us. It’ll will be very helpful for us to validate the reported query at
our end and provide the solution as early as possible. Kindly refer the
attached sample for your reference.
Sample: https://support.syncfusion.com/attachment/download/385308
Regards,
Sarvesh