How to add validation for inline editing?

1 Answer 8 Views
Form Grid ValidationMessage
Bogdan
Top achievements
Rank 1
Bogdan asked on 02 Sep 2025, 07:36 PM | edited on 02 Sep 2025, 07:37 PM

Hi guys,

I have a grid with inline editing and I want to add some custom validation, as you can check below.

<TelerikGrid Data="@Model.Equipments"
             EditMode="GridEditMode.Inline"
             OnUpdate="@OnUpdate"
             OnCreate="@OnCreate"
             OnDelete="@OnDelete">
    <GridToolBarTemplate>
        <GridCommandButton Command="Add">Add new equipment</GridCommandButton>
    </GridToolBarTemplate>
    <GridColumns>
<GridColumn Field="@nameof(Equipment.Code)"
                    Title="Code">
            <EditorTemplate>
                @{
                    Equipment item = (Equipment)context;
                }
                <div class="k-form-field-wrap">
                    <TelerikTextBox @bind-Value="item.Code" />
                    <TelerikValidationMessage For="@(() => item.Code)" />
                </div>
            </EditorTemplate>
        </GridColumn>
        <GridColumn Field="@nameof(Equipment.Name)"
                    Title="Name">
            <EditorTemplate>
                @{
                    Equipment item = (Equipment)context;
                }
                <div class="k-form-field-wrap">
                    <TelerikTextBox @bind-Value="item.Name" />
                    <TelerikValidationMessage For="@(() => item.Name)" />
                </div>
            </EditorTemplate>
        </GridColumn>       
        <GridCommandColumn Width="185px">
            <GridCommandButton Command="Edit">Edit</GridCommandButton>
            <GridCommandButton Command="Delete">Delete</GridCommandButton>
            <GridCommandButton Command="Save" ShowInEdit="true">Save</GridCommandButton>
            <GridCommandButton Command="Cancel" ShowInEdit="true">Cancel</GridCommandButton>
        </GridCommandColumn>
    </GridColumns>
</TelerikGrid>

private void OnCreate(GridCommandEventArgs args)
{
    Equipment item = (Equipment)args.Item!;

    item.Id = Guid.CreateVersion7();

    Model.Equipments.Add(item);
}

private void OnUpdate(GridCommandEventArgs args)
{
    Equipment item = (Equipment)args.Item!;

    int indexToUpdate = Model.Equipments.FindIndex(e => e.Id == item.Id);

    if (indexToUpdate != -1)
    {
        Model.Equipments[indexToUpdate] = item;
    }
}

private void OnDelete(GridCommandEventArgs args)
{
    Equipment item = (Equipment)args.Item!;

    int indexToRemove = Model.Equipments.FindIndex(e => e.Id == item.Id);

    if (indexToRemove != -1)
    {
        Model.Equipments.RemoveAt(indexToRemove);
    }
}



public sealed class Equipment
{
    public Guid Id { get; set; }

    [Required]
    public string? Name { get; set; }

    [Required]
    public string? Code { get; set; }
}

If I don't fill in the Name or Code, I and I hit Save, I can see the validation message under the corresponding column in the row that is in edit mode. So far so good.

However, say that I want the code to be unique. How to I tackle this one? I would like to see a validation message under the Code textbox "That code already exists", or whatever the message might be.

Any guidance on the recommended approach here would be great—thanks!

Regards,

Bogdan

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimo
Telerik team
answered on 03 Sep 2025, 06:08 AM

Hello Bogdan,

Consider the following alternative options:

  • Implement a custom DataAnnotationsValidator. It may be a bit cumbersome or difficult to retrieve the current collection of Codes inside the IsValid() method.
  • Verify the uniqueness in the OnUpdate handler and cancel the event conditionally. You will also need to show a custom validation message, as this validation will not go through the standard .NET validation mechanism.

Regards,
Dimo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Bogdan
Top achievements
Rank 1
commented on 03 Sep 2025, 10:14 AM

Hi Dimo,

Thanks for the quick reply and the suggestions.

Since what I want is not possible, I will just stick with displaying the message in a pop-up.

Regards,

Bogdan

Tags
Form Grid ValidationMessage
Asked by
Bogdan
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or