Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo-Grid column field validation

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.

like image 614
Anil Arya Avatar asked Jul 10 '14 08:07

Anil Arya


2 Answers

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.

like image 199
gitsitgo Avatar answered Oct 01 '22 00:10

gitsitgo


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;
        }
like image 29
Rajakrishna Avatar answered Oct 01 '22 00:10

Rajakrishna