gpt4 book ai didi

javascript - Kendo UI Validator - 处理具有相同名称属性的输入

转载 作者:行者123 更新时间:2023-11-30 05:31:01 25 4
gpt4 key购买 nike

我目前有一个类似于下面的表格:

<form action="/" method="post" id="myForm">
<div class="row">
<input type="text" name="rowValue" class="rowValue">
</div>
<div class="row">
<input type="text" name="rowValue" class="rowValue">
</div>
<div class="row">
<input type="text" name="rowValue" class="rowValue">
</div>
<div class="row">
<input type="text" name="rowValue" class="rowValue">
</div>
<input type="submit" value="Submit">
</form>

一些背景知识:JS 用于将 X 数量的新“行”注入(inject)表单。

我尝试使用:

var myForm = $('#myForm').kendoValidator({
/* rules/messages go here*/
}).data('kendoValidator');

myForm.validate();

我只在第一个 input[name='rowValue'] 上看到一条错误消息。

JS Fiddle

我怀疑 Kendo Validator 需要唯一的名称属性才能正确验证。这是一种耻辱,因为许多后端语言都能够接受相同的名称属性,因为它们连接值或将它们转换为数组或集合 (ASP.NET)。

有没有办法让 Kendo UI Validator 验证具有相同名称属性的表单字段?

最佳答案

您的猜测是正确的。您可以像这样为您的用例调整验证器:

kendo.ui.Validator.prototype.validateInput = function (input) {
input = $(input);

var that = this,
template = that._errorTemplate,
result = that._checkValidity(input),
valid = result.valid,
className = ".k-invalid-msg",
fieldName = (input.attr("name") || ""),
lbl = input.parent().find("span" + className).hide(),
messageText;

input.removeAttr("aria-invalid");

if (!valid) {
messageText = that._extractMessage(input, result.key);
that._errors[fieldName] = messageText;
var messageLabel = $(template({
message: messageText
}));

that._decorateMessageContainer(messageLabel, fieldName);

if (!lbl.replaceWith(messageLabel).length) {
messageLabel.insertAfter(input);
}
messageLabel.show();

input.attr("aria-invalid", true);
}
input.toggleClass("k-invalid", !valid);

return valid;
};

请注意,此方法中有一些简化,因此它可能会在某些特殊情况下中断。

( demo )

关于javascript - Kendo UI Validator - 处理具有相同名称属性的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com