当您在未使用模板的网格中单击“编辑”时,您为 schema.Model 定义的验证规则将得到正确应用。但如果您使用自定义模板,则不会应用 schema.Model 验证规则。
我怀疑答案是因为我使用自定义弹出编辑模板以便我可以有一个下拉列表,所以我必须手动指定每个 html 输入字段的规则,例如必需的。我希望这不是真的,并且定义 schema.Model 的相同规则也适用于编辑器模板。
知道答案的请留言,谢谢!
担
@丹尼尔
这是我的代码,用于获取模型中定义的验证属性并将它们添加到 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>');
}
}
}
});
}
另外,为了以防万一,我设置了一个在必填字段的标签之前添加 * 的类,以及在每个非必填字段之后添加文本“可选”的类。 KEY、ID 和 STAMP 是我的模型中定义的字段,但我没有将它们放在表单上,因此您可以根据需要排除实体关键字段。
他们来了
.required:before {
content: "*";
color: red
}
.optional:after {
content: " (optional)";
color: #777;
}
use required
在您的输入元素中
<input type="text" class="k-input k-textbox" name="Name" data-bind="value:Name" required>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)