kendoUI网格弹出编辑模板验证没有获取模型验证规则

时间:2013-01-23 01:09:38

标签: kendo-ui

在未使用模板的网格中单击“编辑”时,将为schema.Model定义的验证规则得到正确应用。但是,如果使用自定义模板,则不会应用schema.Model验证规则。

我怀疑答案是因为我使用自定义弹出式编辑模板,因此我可以有一个下拉列表,我必须手动指定每个html输入字段的规则,例如required。我希望这不是真的,并且定义schema.Model的相同规则也适用于编辑器模板。

如果您知道答案,请发帖,谢谢! 丹

@Daniel 这是我获取模型中定义的验证属性并将它们添加到DOM

的代码
/**
 * Sets label,input html5 attributes and css based on the validation attributes of the
 * model for the given dataSource
 * @param {Object} myDs    dataSource that contains the Model Schema used for validation.
 */
function addValidationAttributes(myDs) {

    var myFields
    //Pass in DS or pass in model from grid
    if (myDs.options) {
        myFields = myDS.options.schema.model.fields
    } else//model
    {
        myFields = myDs.fields
    }

    $.each(myFields, function(fieldName) {
        //Add validation attributes
        if (this.validation) {
            $('#' + fieldName).attr(this.validation);
            //Set label to required if field is required
            if (this.validation.required) {
                $('label[for=' + fieldName + ']').addClass('required');
                $('#' + fieldName).attr('title', "required");
                //Check if KendoUI widget because it creates an extra span
                if ($('#' + fieldName).is('[data-role]')) {
                    $('.k-widget').after('<span class="k-invalid-msg" data-for="' + fieldName + '"></span>');
                } else {
                    $('#' + fieldName).after('<span class="k-invalid-msg" data-for="' + fieldName + '"></span>');
                }
            }
        } else//optional
        {
            //Non requried fields set to optional exclude KEY ID STAMP
            if (fieldName !== '__KEY' && fieldName !== '__STAMP' && fieldName !== 'ID') {
                //Check if KendoUI widget because it creates an extra span
                if ($('#' + fieldName).is('[data-role]')) {
                    $('.k-widget').after('<span class="optional" data-for="' + fieldName + '"></span>');
                } else {
                    $('#' + fieldName).after('<span class="optional" data-for="' + fieldName + '"></span>');
                }
            }
        }

    });
}

另外,如果你想要它,我设置一个类,在所需字段的标签之前添加*,在每个没有必填字段之后添加文本“Optional”。 KEY,ID和STAMP是我的模型中定义的字段,但我没有将它们放在表单上,​​因此您可以根据需要排除实体键字段。

他们在这里

.required:before {
    content: "*";
    color: red
}

.optional:after {
    content: " (optional)";
    color: #777;
}

1 个答案:

答案 0 :(得分:2)

在输入元素中使用required

<input type="text" class="k-input k-textbox" name="Name" data-bind="value:Name" required>
相关问题