Jquery validate hides kendo-ui controls

夙愿已清 提交于 2019-12-04 22:29:44

问题


I have a form which uses a kendo-ui numericTextBox

@Html.LabelFor(p => p.Cost)
@Html.TextBoxFor(p => p.Cost, new { @autocomplete = "off" })

I bind it, then, to make it work with jquery validate plugin, i set the following settings:

$("#Cost").kendoNumericTextBox({
    format: "c",
    min: 0,
    decimals: 2
});

$.validator.setDefaults({
    ignore: [],
    highlight: function (element, errorClass) {
        element = $(element);
        if (element.hasClass("k-input")) {
            element.closest(".k-widget").addClass(errorClass);

        } else {
            element.addClass(errorClass);
        }
    },
    unhighlight: function (element, errorClass) {
        element = $(element);
        if (element.hasClass("k-input")) {
            element.closest(".k-widget").removeClass(errorClass);
        } else {
            element.removeClass(errorClass);
        }
    }
});

When i try to submit the form and Cost input is invalid, it adds the errorClass properly (on .k-widget wrapper).

The problem is that, if i press the submit button again, then the kendo-ui element simply disappears (with style="display: none;").

I don't know what is triggering this. I've seen that if i change the errorClass to something else other than input-validation-error, then the kendo-ui widget remains visible.

This issue happens only with kendo-ui controls, not also with standard html inputs.

I am doing something wrong?


回答1:


I'm betting that the numeric texbox control is double-div-wrapped just like the datepicker control is. Here are the highlight() and unhighlight() functions I use in my validator configuration to determine what element to apply the error class to:

...
highlight: function (element, errorClass, validClass) {
  var e = $(element),
      parent = _getParent(e);

    _addClass(e, parent);
  },
unhighlight: function (element, errorClass, validClass) {
  var e = $(element),
      parent = _getParent(e);

  _removeClass(e, parent);
}
...

function _getParent(element) {
  // a Kendo DatePicker is double-wrapped, so that requires us to return the parent of the parent
  return (element.parent().hasClass('k-picker-wrap')) ? element.parent().parent() : element.parent();
}

function _addClass (element, parent) {
  if (parent.hasClass('k-widget')) {
    parent.addClass('error');
  } else {
    element.addClass('error');
  }
}

function _removeClass(element, parent) {
  if (parent.hasClass('k-widget')) {
    parent.removeClass('error');
  } else {
    element.removeClass('error');
  }
}



回答2:


To fix the problem that the element disappears on the second submit, I did this:

$("form").submit(function (event) {
  $(".k-widget").removeClass("input-validation-error");
}


来源:https://stackoverflow.com/questions/16633498/jquery-validate-hides-kendo-ui-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!