首页 > 解决方案 > How does kendo form validation custom required work?

问题描述

I would like to add a required field to a Kendo form. This works with:

validation: { required: true },

If I now want to set required myself in a function, this no longer works.

validation: { 
  required: function(){
             return true;
            }  
},

I have created an example: https://dojo.telerik.com/epEzewIt/5

If no entry is made in the "MultiSelect" field, the required variant as a function is no longer displayed: "MultiSelect is required" when it is empty.

How can I store required with a function?

标签: validationkendo-ui

解决方案


If you want to execute some code one time you can use an anonymous function like:

validation: { 
    required: (function(){
        return true; // run your rules here
    })()

But, as documentation suggests, those (required, max/min, pattern, etc.) are field attributes that map to HTML rules. So you can't pass a function.

Why not use the kendoValidator on your submit action. This can be used to validate all the fields and set custom messages.

Here are more details on how to use Kendo Validator to create custom validations: https://docs.telerik.com/kendo-ui/controls/editors/validator/rules

There are a lot of examples there.


推荐阅读