Bootstrap twitter 3.0和knockoutjs验证

时间:2013-08-23 12:18:40

标签: javascript css twitter-bootstrap knockout.js

使用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的方式来配置Knockout-Validation?

3 个答案:

答案 0 :(得分:56)

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

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>

答案 1 :(得分: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() });
        }
    }
};

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

答案 2 :(得分:2)

您将在bootstrap form 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>

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

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

这是我在工具中的方式。