Kendo Validator显示ComboBox的错误默认消息

时间:2017-08-15 13:48:45

标签: javascript asp.net-mvc kendo-ui

INPUT ELEMENTS中的消息从数据注释中正确填充,但KendoValidator似乎创建了不同的东西。

  • 问:为什么会这样?
  • 问:如何解决? - 我愿意通过JavaScript进行更新

为了简单起见,让我们看看其中一个下拉菜单......

HTML
请注意data-val-required如何包含实际正确的消息?

<span class="k-widget k-combobox k-header" style="width: 100%;">
    <span tabindex="-1" unselectable="on" class="k-dropdown-wrap k-state-default input-validation-error">
        <input name="Entity.DeviceTypeId_input" class="k-input k-valid" type="text" autocomplete="off" role="combobox" aria-expanded="false" placeholder="Select Device Type..." tabindex="0" aria-disabled="false" aria-readonly="false" aria-autocomplete="both" aria-owns="Entity_DeviceTypeId_listbox" aria-busy="false" aria-activedescendant="bec66a14-dad2-4632-84ba-02a9e3b5a10d" style="width: 100%;">
        <span tabindex="-1" unselectable="on" class="k-select">
            <span unselectable="on" class="k-icon k-i-arrow-s" role="button" tabindex="-1" aria-controls="Entity_DeviceTypeId_listbox">select</span>
        </span>
    </span>
    <input data-val="true" data-val-number="The field DeviceTypeId must be a number." data-val-required="Device Type is required." id="Entity_DeviceTypeId" name="Entity.DeviceTypeId" required="required" style="width: 100%; display: none;" type="text" aria-required="true" data-role="combobox" aria-disabled="false" aria-readonly="false" aria-invalid="true" class="k-invalid">
</span>

数据注释:
如您所见,data-val-required中的消息正确无误......

public class DeviceAnnotations
{
    [Required(ErrorMessage = "Device Type is required.")]
    public object DeviceTypeId { get; set; }

    [Required(ErrorMessage = "State is required.")]
    public object StateId { get; set; }
}

JAVASCRIPT:
我愿意更新JavaScript,但我更愿意理解为什么&amp;错误的消息来自......

var validationRoutine = {
    validate: function (e) {

        var comboBoxes = $(".k-combobox");

        $.each(comboBoxes, function (key, value) {

            var $input = $(value).find("input.k-invalid");    //<-- this is where Kendo foolishly places k-invalid
            var $span = $(this).find("span.k-dropdown-wrap"); //<-- this span controls the dropdown's appearance.

            if ($input.length > 0) { // k-invalid exists...
                $span.addClass("input-validation-error");
                return;
            }

            $span.removeClass("input-validation-error");
        });
    }
};

$('form').kendoValidator(validationRoutine);

屏幕拍摄:
enter image description here

1 个答案:

答案 0 :(得分:0)

不确定这一点,但之所以会发生这种情况,是因为您拥有required属性,并且根据您的name="Entity.DeviceTypeId"显示错误消息

  

实体DeviceTypeId是必需的

你能否尝试添加

validationMessage="Device Type is required."

input

我认为这应该有效,我已经使用过但不是asp