1

我一直在查看一些关于在下拉列表中使用 BootStrapValidtor 的教程并找到了一个示例,但它无法验证下拉列表,而且我也遇到了验证 kendoUI 多选的问题。

现在我的多选标记是这个

<div class="form-group">
    <label for="ddAdministrationManufacturerCatalog" class="control-label col-md-2" id="lblAdministrationClientCatalogMultiSelect"><b>Catalog</b></label>
    <div class="col-md-8">
        <select id="msManufacturerCatalogs" multiple name="catalog"></select>
    </div>
</div>

在我的这个多选的验证器脚本中,特别是这个..

catalog: {
    message: "Catalog is required",
    validators: {
        notEmpty: {
            message: "Please choose a catalog"
        }
    }
}

我对剑道下拉列表的标记是这样的

<div class="form-group">
    <label for="acCountries" class="control-label col-md-2" id="lblAdministrationManufacturerCountry"><b>Country</b></label>
    <div class="col-md-10">
        <select id="acCountries" class="form-control" name="country"></select>
    </div>
</div>

它的验证器脚本部分是这个

country: {
    message: "Country is required",
    validators: {
        notEmpty: {
            message: "Please provide a Country"
        },
        greaterThan: {
            value: 0,
            message: "Required"
        }
    }
}

我正在创建多选

function CatalogDropDown(manufacturerCatalogs) {
    $("#msManufacturerCatalogs").kendoMultiSelect({
        dataSource: manufacturerCatalogs,
        dataTextField: "CatalogName",
        dataValueField: "CatalogID"
    });
}

我的下拉列表是这个

function ShowCountries(countryData) {
    $("#acCountries").kendoDropDownList({
        dataSource: countryData,
        dataTextField: "dictionaryName",
        dataValueField: "dictionaryItemID",
        animation: {
            close: {
                effects: "zoom:out",
                duration: 500
            }
        },
        optionLabel: {
            dictionaryName: "-- Select Country --",
            dictionaryItemID: "0"
        }
    });
}
4

1 回答 1

0

我建议使用Kendo UI Validator但如果你仍然想使用bootstrapValidator那么你需要

  1. bootstrapValidator 最新版本0.5.3,可以在这里找到
  2. 您必须使用插件查找、检查和验证kendoMultiSelectHTMLkendoDropDownList元素bootstrapValidator,否则验证将不起作用
  3. 您需要bootstrapValidator callbackchange功能来验证字段并检测其中的任何更改。在这种情况下kendoDropDownList

kendo plugin这是带有和的通用示例bootstrapValidator

$(document).ready(function () {
  $('#Form')
    .find('[name="country"]')
    .kendoDropDownList({
      animation: {
        close: {
          effects: "zoom:out",
          duration: 500
        }
      },
    })
    // Revalidate the country when it is changed
    .change(function (e) {
      $('#Form').bootstrapValidator('revalidateField', 'country');
    })
    .end()
    .find('[name="catalog"]')
    .kendoMultiSelect()
    // Revalidate the catalog when it is changed
    .change(function (e) {
      $('#Form').bootstrapValidator('revalidateField', 'catalog');
    })
    .end()
    .bootstrapValidator({
    excluded: ':disabled', //<--- You need to exclude disabled fields
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      country: {
        message: "Country is required",
        validators: {
          notEmpty: {
            message: "Please provide a Country"
          },
          greaterThan: {
            value: 0,
            message: "Required"
          }
        }
      },
      catalog: {
        validators: {
          callback: {
            message: "Catalog is required",
            callback: function (value, validator) {
              // Get the selected options and minimum 2 are required
              var options = validator.getFieldElements('catalog').val();
              return (options != null && options.length >= 2 && options.length <= 4);
            }
          }
        }
      }
    }
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.css"/>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2014.1.318/styles/kendo.common.min.css"/>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2014.1.318/styles/kendo.blueopal.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2014.1.318/js/kendo.all.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.js"></script>

<form id="Form">
  <div class="form-group">
    <label for="ddAdministrationManufacturerCatalog" class="control-label col-md-2" id="lblAdministrationClientCatalogMultiSelect"><b>Catalog</b></label>
    <div class="col-md-8">
      <select id="msManufacturerCatalogs" class="form-control" multiple name="catalog">
        <option value="1">Steven White</option>
        <option value="2">Nancy King</option>
        <option value="3">Nancy Davolio</option>
        <option value="4">Robert Davolio</option>
        <option value="5">Michael Leverling</option>
        <option value="6">Andrew Callahan</option>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label for="acCountries" class="control-label col-md-2" id="lblAdministrationManufacturerCountry"><b>Country</b></label>
    <div class="col-md-10">
      <select id="acCountries" class="form-control" name="country">
        <option>Select</option>
        <option value="1">Michael Suyama</option>
        <option value="2">Anne King</option>
        <option value="3">Laura Peacock</option>
        <option value="4">Robert Fuller</option>
        <option value="5">Janet White</option>
      </select>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-offset-3 col-md-8">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

小提琴示例

于 2015-12-02T14:38:02.813 回答