Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery validate hides kendo-ui controls

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?

like image 797
Catalin Avatar asked May 19 '13 10:05

Catalin


1 Answers

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');
  }
}
like image 87
Brett Avatar answered Sep 28 '22 08:09

Brett