Issue with [Required] Validation on Grid control

HI, could you please help me with issue that I face.

I'm using your example code from this thread  Issue with [Required] Validation | Blazor Forums | Syncfusion®

This is the code :

@page "/itemtypes"


@using Syncfusion.Blazor.Grids

<Syncfusion.Blazor.Grids.SfGrid DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">

    <Syncfusion.Blazor.Grids.GridEditSettings AllowEditing="true" AllowAdding="true" Mode="EditMode.Normal"></Syncfusion.Blazor.Grids.GridEditSettings>

    <Syncfusion.Blazor.Grids.GridColumns>

        <Syncfusion.Blazor.Grids.GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></Syncfusion.Blazor.Grids.GridColumn>

        <Syncfusion.Blazor.Grids.GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" ValidationRules="@(new ValidationRules{ Required= true, MaxLength = 10 })" Width="100" MinWidth="100" MaxWidth="100"></Syncfusion.Blazor.Grids.GridColumn>

        <Syncfusion.Blazor.Grids.GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></Syncfusion.Blazor.Grids.GridColumn>

        <Syncfusion.Blazor.Grids.GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" ValidationRules="@(new ValidationRules{ Required= true })" TextAlign="TextAlign.Right" Width="120"></Syncfusion.Blazor.Grids.GridColumn>

    </Syncfusion.Blazor.Grids.GridColumns>

    <Syncfusion.Blazor.Grids.GridEvents TValue="Order" OnActionBegin="ActionBegin"></Syncfusion.Blazor.Grids.GridEvents>

</Syncfusion.Blazor.Grids.SfGrid>


@code {


    List<Order> Orders { get; set; }


    protected override void OnInitialized()

    {

        Orders = Enumerable.Range(1, 10).Select(x => new Order(1000 + x)

        {

            OrderID = 1000 + x,

            CustomerID = (new string[] { "ALFKI",

                "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],

            Freight = (new double[] { 2, 1, 4, 5, 3 })[new Random().Next(5)] * x,

            OrderDate = (new DateTime[] { new DateTime(2019, 01, 01), new DateTime(2019, 01, 02) })[new Random().Next(2)]

        }).ToList();

    }


    public void ActionBegin(ActionEventArgs<Order> arg)

    {

        //Handles add operation

        if (arg.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add))

        {

            arg.Data = new Order(0) { CustomerID = "Customer ID" };

        }


        //Handles edit operation. During edit operation, original object will be cloned.

        if (arg.RequestType.Equals(Syncfusion.Blazor.Grids.Action.BeginEdit))

        {

            arg.Data = new Order(arg.RowData.OrderID)

            {

                CustomerID = arg.RowData.CustomerID,

                Freight = arg.RowData.Freight,

                OrderDate = arg.RowData.OrderDate

            };

        }

    }


    // This class does not contain any parameter-less constructor, hence this cannot be instantiated using Activator.CreateInstance.

    public class Order

    {

        public Order(int orderid) => OrderID = orderid;


        public int OrderID { get; set; }

        public string CustomerID { get; set; }

        public DateTime? OrderDate { get; set; }

        public double? Freight { get; set; }

    }

}

Im not sure I run your razor its works fine, but on my project its not working.

When adding even after input values still have required message. but when editing everything works well.
Image_7909_1744290100470Syncfusion NuGet version =29.1.33
visual studio version details = Visual studio 2022 Enterprise Version 17.13.5

Target framework .NET 9.0

Packages that I use
Image_1085_1744290206433

I'm not sure what's going on in here, when I delete the required, everything was fine.
But if I work on my Project, I need some validation, that's why this Required really matters.


1 Reply

SK Sanjay Kumar Suresh Syncfusion Team April 17, 2025 01:55 PM UTC

Hi Dev Modest,


While checking the provided sample, we noticed that a new instance is being created during the add operation. Because of this, the instance is not being tracked properly by the EditContext, which causes the validation messages to be repeatedly triggered.


To resolve this issue, we recommend modifying the existing property values directly instead of creating a new instance and assigning it within the args.Data as like below..


Concern Code Snippet:


public void RowCreatingHandler(Syncfusion.Blazor.Grids.RowCreatingEventArgs<Order> args)

{

     args.Data.CustomerID = "Updated";

     // args.Data = new Order() { CustomerID = "Customer ID" };

}


Playground Sample:

https://blazorplayground.syncfusion.com/embed/VthyNzWwJQVKQVck?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


we would like to inform you that the ActionBegin event for CRUD actions will soon be deprecated. We have introduced separate events for each CRUD operation, and it is recommended that you handle specific actions with the new events instead. In your case, we suggest using the RowUpdating event to achieve your requirement.

References:



New Event Information

Event Name

Argument Name

Properties

Description

RowCreating

RowCreatingEventArgs

Cancel, Data, Index, EditContext

Gets or sets the event callback that is raised before the add action is performed in the grid.

RowCreated

RowCreatedEventArgs

Data, Index, EditContext

Gets or sets the event callback that is raised after the add action is performed in the grid.

RowUpdating

RowUpdatingEventArgs

Cancel, IsShiftKeyPressed, KeyCode, Data, Index, PreviousData, PrimaryKeys, PrimaryKeyValue

Gets or sets the event callback that is raised before the save action is performed in the grid.

RowUpdated

RowUpdatedEventArgs

Data, Index, PreviousData, PrimaryKeys, PrimaryKeyValue

Gets or sets the event callback that is raised after the save action is performed in the grid.

RowDeleting

RowDeletingEventArgs

Cancel, Datas, PrimaryKeys

Gets or sets the event callback that is raised before the delete action is performed in the grid.

RowDeleted

RowDeletedEventArgs

Datas, PrimaryKeys

Gets or sets the event callback that is raised after the delete action is performed in the grid.

EditCanceling

EditCancelingEventArgs

Cancel, Data, PreviousData, PrimaryKeys, Index

Gets or sets the event callback that is invoked before the cancel action is performed in the grid, specifically when using normal and dialog edit modes.

EditCanceled

EditCanceledEventArgs

Data, PreviousData, PrimaryKeys, Index

Gets or sets the event callback that is invoked after the cancel action is performed in the grid, specifically when using normal and dialog edit modes.

OnRowEditStart

OnRowEditStartEventArgs

Cancel, PreventDataClone

Gets or sets the event callback that is raised before an editing action is performed in the grid.

RowEditing

RowEditingEventArgs

Cancel, PrimaryKeys, PrimaryKeyValue, Data, Index, EditContext, ForeignKeyData

Gets or sets the event callback that is raised before the edit action is performed in the grid.

RowEdited

RowEditedEventArgs

PrimaryKeys, PrimaryKeyValue, Data, Index, EditContext, ForeignKeyData

Gets or sets the event callback that is raised after the edit action is performed in the grid.




Regards,

Sanjay Kumar Suresh


Loader.
Up arrow icon