Kendo-Grid column field validation

23,140

Solution 1

There isn't really any error in your code, but more like an error in Kendo Grid's validation design. Even though you specify the validation function only in the title field, it will run the validation globally for any input field that you edit.

In validateTitle you need to filter which input you want the validating function to run on. Something like this:

if (input.is("[name='title']") && input.val().length > 5) {
    input.attr("data-validateTitle-msg", "Max length exceeded 5 characters only");
    return false;
}

If you need a live working demo, you can always refer to Telerik's online demos that are editable, very handy for playing around with things. Here's the demo for custom validation where they similarly have to filter the input for the field name.

Solution 2

enter image description here

you want simply required field validation means just add your view model property attributes

[Required(ErrorMessage ="CountryCode is Mandatory")]
        public virtual string CountryCode
        {
            get;
            set;
        }

Solution 3

We can easily set the maximum length using this code,It will not allow user to enter more characters than the specified one

  model: {
                    id: "CLASSID",
                    fields: {
                        CLASSID: { type: "number" },
                        CLSNAME: { type: "string" },
                        CLSFLAG: {
                            type: "string", validation: {
                                required: true,maxlength:"3"
                            }
                        },
                        CLSSTATUS: { type: "boolean" }
                    }
                }
Share:
23,140
Anil Arya
Author by

Anil Arya

Updated on November 19, 2020

Comments

  • Anil Arya
    Anil Arya over 3 years

    I am working on populating kendo--grid with APIs data but on adding validation on one field is automatically working for every other fields too.

    Here is schema inside kendo-dataSource :

    schema: {
                       model: {
                           id : "id",
                           fields: {
                               id: { editable: false, type: 'number'},
                               name: { editable: true, type : "string" },
                               unique_url: { editable: true , type: 'string'},
                               image_url : { editable: true, type : "string" },
                               title: {type : "string", validation: {
                                                    required: true,
                                                    validateTitle: function (input) {
                                                        console.log("I am inside validation",input.val());
                                                        if (input.val().length > 5) {
                                                           input.attr("data-validateTitle-msg", "Max length exceeded 5 characters only");
                                                           return false;
                                                        }    
    
                                                        return true;
                                                    }
                                                }
                                                },
                               body: { editable: true, type : "string",validation: { max: 90, required: true, message : "Maximum characters should be 90"} },
                               adaccount_id: { editable: false, type: 'number'}
                           }
                       }
                    },  
    

    Here I have added validation for title field but its getting called for others fields too. I am adding one snapshot of validation--- enter image description here

    Please help me to find errors in it.

  • Gerry
    Gerry over 3 years
    "an error in Kendo Grid's validation design" - you nailed it on the head there! The design is completely broken!