angularJS valdr+ui选择验证

angularJS valdr+ui选择验证,angularjs,angularjs-directive,ui-select,angularjs-ui-utils,valdr,Angularjs,Angularjs Directive,Ui Select,Angularjs Ui Utils,Valdr,我在谷歌上搜索过这个问题,但仍然找不到正确的讨论。我们在表单angularJS应用程序中使用valdr和ui select,我们面临的问题是,ui select呈现的输入不会获得名称属性,因此angular抛出一个错误: Error: Form element with ID "undefined" is not bound to a field name. at d.link (http://localhost:8080/bower_components/valdr/valdr.min.js:

我在谷歌上搜索过这个问题,但仍然找不到正确的讨论。我们在表单angularJS应用程序中使用valdr和ui select,我们面临的问题是,ui select呈现的输入不会获得名称属性,因此angular抛出一个错误:

Error: Form element with ID "undefined" is not bound to a field name.
at d.link (http://localhost:8080/bower_components/valdr/valdr.min.js:1:8414)
at invokeLinkFn (http://localhost:8080/bower_components/angular/angular.js:8141:9)
at nodeLinkFn (http://localhost:8080/bower_components/angular/angular.js:7653:11)
at compositeLinkFn (http://localhost:8080/bower_components/angular/angular.js:7009:13)
at nodeLinkFn (http://localhost:8080/bower_components/angular/angular.js:7648:24)
at http://localhost:8080/bower_components/angular/angular.js:7884:13
at processQueue (http://localhost:8080/bower_components/angular/angular.js:13071:27)
at http://localhost:8080/bower_components/angular/angular.js:13087:27
at Scope.$get.Scope.$eval (http://localhost:8080/bower_components/angular/angular.js:14287:28)
at Scope.$get.Scope.$digest (http://localhost:8080/bower_components/angular/angular.js:14103:31) <input type="text" autocomplete="off" tabindex="-1" aria-expanded="true" aria-label="{{ $select.baseTitle }}" aria-owns="ui-select-choices-{{ $select.generatedId }}" aria-activedescendant="ui-select-choices-row-{{ $select.generatedId }}-{{ $select.activeIndex }}" class="form-control ui-select-search ng-pristine ng-untouched ng-valid" placeholder="{{$select.placeholder}}" ng-model="$select.search" ng-show="$select.searchEnabled &amp;&amp; $select.open">
因此,我们在ui select指令上尝试了一些技巧,比如templateCache rewrite/modify,使用相同的模型隐藏输入,但结果是相同的

顺便说一句,templateCache重写是最糟糕的方法,因为这个指令在appwide中被其他指令使用,我们不能对整个应用进行黑客攻击

有人遇到过这个问题吗

代码段:

如果输入元素上不存在name属性,valdr将引发异常

ui select在内部创建一个没有名称属性的输入字段。因此出现了错误

directive('input', function () {
    var count = 0;
    return {
      restrict: 'E',
      compile: function () {
        return {
          pre: function (scope, element, attrs) {
            // if the input is created by ui-select and has ng-model
            if (element.hasClass('ui-select-search') && attrs.ngModel) {
              if (!attrs.name) { // if the input is not having any name
                attrs.name = 'ui-select' + (++count); // assign name
              }
            }
          }
        }
      }
    }
  })
您可以尝试此操作以消除错误

directive('input', function () {
    var count = 0;
    return {
      restrict: 'E',
      compile: function () {
        return {
          pre: function (scope, element, attrs) {
            // if the input is created by ui-select and has ng-model
            if (element.hasClass('ui-select-search') && attrs.ngModel) {
              if (!attrs.name) { // if the input is not having any name
                attrs.name = 'ui-select' + (++count); // assign name
              }
            }
          }
        }
      }
    }
  })

您声称ui select呈现不会获得name属性,但错误消息表明您的输入字段没有ID:ID未定义的Form元素没有绑定到字段名,不是吗?也许您应该创建一个小的Plunkr来显示这种行为。从这里开始:我来检查一下。它告诉我ID丢失了,但正如我看到的,输入的ID与名称相同,我添加了代码片段仅供参考:如果输入上没有名称,最新的valdr版本1.1.5不再引发异常。看:谢谢,我们很快会查的。保持联系,我查过了。真的很管用,我们拭目以待。明天我们将在这个项目上测试这个想法。这是对的,但我感到震惊的是,它有一种类似黑客的方式。从维护的角度来看,它不确定这是最好的选择。@DánielSebestyén,你是对的。这是一个黑客。但是为了消除这个错误,我们需要对这些库、valdr库或hack库中的任何一个进行更改。我们决定同意并接受这个解决方案。我认为这两个项目的作者和贡献者都应该关注这个问题。谢谢你的帮助。