This is a migrated thread and some comments may be shown as answers.

Length validation in cell does not work on Kendo Grid cell edit

2 Answers 849 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
Daochuen asked on 01 Oct 2020, 03:25 PM

Hello,

I defined my class field “TeamCode” as string type and it only accept 4 characters.  I want edit box stop accept any characters more than it's length limitation. if user try to enter 5th character in the box the cursor should not move since max length defined on that box is 4.  It works well in regular Kendo TextBox. But it does not work on Kendo grid cell when do the same thing. Length limitation does not happen in the cell except when cell lose cursor. Is anyone have an idea to make it work? (Below are partial of code). Thanks in advance.

 

Field in class:

            [StringLength(4, MinimumLength = 4, ErrorMessage = "{0} must be 4 characters.")]

             [DisplayName("Team Code")]

             public string TeamCode { get; set; }

 

Code take action in individual textbox:

@Html.Kendo().TextBoxFor(model => model.TeamCode).HtmlAttributes(new { maxlength = 4})

 

 

Code does not take action in Kendo Grid cell editor:

...

columns.Bound(m => m.TeamCode).Width(100).HtmlAttributes(new { maxlength = "4"});

...

2 Answers, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 05 Oct 2020, 12:27 PM

Hi Daochuen,

when the Grid is in Edit mode it uses the Editor templates located in the ~/Views/Shared/EditorTemplates folder to render the widgets used for editing. That said you can create a custom editor as explained in the documentation section below

https://docs.telerik.com/aspnet-mvc/html-helpers/data-management/grid/editing/custom

Then add a UIHint attribute to the model field so that when the grid is in edit mode the custom editor will be used:

        [StringLength(4, MinimumLength = 4, ErrorMessage = "{0} must be 4 characters.")]
        [DisplayName("Team Code")]
        [UIHint("TeamCodeEditor")]
        public string TeamCode
        {
            get;
            set;
        }

TeamCodeEditor.cshtml:

object

@Html.Kendo().TextBoxFor(model => model).HtmlAttributes(new { maxlength = 4 })

You can also review the Custom Editor Demo, for additional details:

https://demos.telerik.com/aspnet-core/grid/editing-custom

Give this suggestion a try and let me know if the issue is resolved.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 07 Oct 2020, 07:21 PM
Thanks, that works!
Tags
Grid
Asked by
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Aleksandar
Telerik team
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
Share this question
or