We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Can Column validation span multiple rows?

Column Validation in Blazor DataGrid Component | Syncfusion

I am currently using a version of a Validator class based off of the example documentation above where I check the input of the current row/index that is being created or edited. However, I wanted to see if there are additional tools to help me check the index/rows that would not only check the current index/row, but also the ones proceeding and after the current row/index. My project currently has a grid featuring a Min and Max column which both represent a range. The min and max are represented by the fields 'RangeFrom' and 'RangeTo' I have code that prevents the save action IF the current row/index's range is Greater than the range on row after it OR Less than the range on the row before it. However, I wish to include the example above's validation messages that warn the user why they cannot save. I hope this makes sense. I will show my razor component and validator class below.

Razor File:

@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? grid;

int level;

string? TableName;

IEnumerable? tableNameList;

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>("https://localhost:7120/ConfigurationTableName");

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("https://localhost:7120/ConfigurationTableName", tableName);

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 args)

{

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 args)

{

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;

}

}

}

}

}

}

}




Validator File:

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)

{


}

}

}




2 Replies

SP Sarveswaran Palani Syncfusion Team December 20, 2022 05:26 AM UTC

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




SP Sarveswaran Palani Syncfusion Team January 9, 2023 05:06 AM UTC

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


Loader.
Live Chat Icon For mobile
Up arrow icon