29

使用 Twitter Bootstrap 验证类,例如has-errorhas-warning需要放在包装form-group元素上,以便设置输入元素及其标签的样式。但是 Knockout-Validation 将类添加到输入元素中。

<div class="form-group has-error">
    <label class="control-label">Input with error</label>
    <input type="text" class="form-control">
</div>

是否可以配置淘汰验证以将类添加到div而不是input

4

3 回答 3

57

thebringking 的答案并没有让你一路走好。您还需要指定errorMessageClassanddecorateInputElement选项。

ko.validation.init({
  errorElementClass: 'has-error',
  errorMessageClass: 'help-block',
  decorateInputElement: true
});

var viewModel = ko.validatedObservable({
  name: ko.observable().extend({
    required: true
  }),
  email: ko.observable().extend({
    required: true,
    email: true
  }),
  submit: function() {

    if (!this.isValid()) {
      this.errors.showAllMessages();
    } else {
      alert('good job');
    }
  }
});

ko.applyBindings(viewModel);
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet" />


<div class="container">
  <form class="form-horizontal">
    <div class="form-group" data-bind="validationElement: name">
      <label class="control-label col-xs-2" for="name">Name</label>
      <div class="col-xs-10">
        <input id="name" class="form-control" type="text" data-bind="textInput: name" />
      </div>
    </div>
    <div class="form-group" data-bind="validationElement: email">
      <label class="control-label col-xs-2" for="email">Email</label>
      <div class="col-xs-10">
        <input id="email" class="form-control" type="text" data-bind="textInput: email" />
      </div>
    </div>
    <div class="form-group">
      <div class="col-xs-offset-2 col-xs-10">
        <button type="submit" class="btn btn-primary" data-bind="click: submit">Submit</button>
      </div>
    </div>
  </form>
</div>


<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>

于 2013-11-30T07:28:47.560 回答
5

您可以像这样扩展淘汰赛验证核心:

var init = ko.bindingHandlers['validationCore'].init;
ko.bindingHandlers['validationCore'].init = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
    var config = ko.validation.utils.getConfigOptions(element);
    // if requested, add binding to decorate element
    if (config.decorateInputElement && ko.validation.utils.isValidatable(valueAccessor())) {
        var parent = $(element).parents(".form-group");
        if (parent.length) {
            ko.applyBindingsToNode(parent[0], { validationElement: valueAccessor() });
        }
    }
};

此代码使父表单组使用与输入相同的类进行装饰。

于 2014-01-20T17:22:16.083 回答
2

您将在引导表单 div 上使用“validationElement”绑定处理程序-

<div class="form-group" data-bind="validationElement: someObservable">
  <label class="control-label" for="inputSuccess">Input with success</label>
  <input type="text" class="form-control" id="inputSuccess">
</div>

然后,您将设置敲除验证插件的配置以使用“has-error”的引导错误类。

ko.validation.init({errorElementClass:'has-error'})

这就是我在我们的工具中执行此操作的方式。

于 2013-08-25T16:36:28.597 回答