【问题标题】:angularJS valdr + ui-select validationangularJS valdr + ui-select 验证
【发布时间】:2015-05-18 18:38:40
【问题描述】:

我在谷歌上搜索过这个问题,但仍然找不到关于它的正确讨论。我们在表单 (angularJS app) 中使用 valdr 和 ui select,我们遇到了 ui-select 呈现的输入不会获得 name 属性的问题,因此,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 指令的 hack,比如 templateCache 重写/修改,隐藏输入具有相同的模型,但结果是相同的。

顺便说一句,templateCache 重写是最糟糕的方法,因为该指令在应用程序范围内被其他指令使用,我们无法破解整个应用程序。

有人遇到过这个问题吗?

代码 sn-p: http://plnkr.co/edit/sDNDDtnhi7Jxi9mtjDTl?p=preview

【问题讨论】:

  • 您声称“ui-select 渲染不会获得名称属性”,但错误消息表明您的输入字段没有 ID:“ID 为“未定义”的表单元素是不绑定到字段名称”,不是吗?也许您应该创建一个显示此行为的小型 Plunkr。从这里开始:plnkr.co/edit/AJC1YL206zmryGx31GFo?p=preview
  • 我会检查的。好吧,它告诉 ID 丢失,但我看到输入的 ID 与其名称相同
  • 我已经添加了代码sn-p
  • 仅供参考:如果输入中没有名称,最新的 valdr 版本 (1.1.5) 不会再抛出异常。见:github.com/netceteragroup/valdr/blob/master/CHANGELOG.md

标签: angularjs angularjs-directive ui-select angularjs-ui-utils valdr


【解决方案1】:

如果输入元素valdr 上不存在name 属性,则抛出异常。

ui-select 在内部创建一个没有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
              }
            }
          }
        }
      }
    }
  })

Working Plnkr

【讨论】:

  • 谢谢,我们会尽快检查。保持联系。
  • 我已经检查过了。真的有效,我们拭目以待。明天我们将在项目上测试这个想法。这个笨蛋是对的,但我害怕它有一种类似hack的方法。到维护时,它不确定这是最好的选择。
  • @DánielSebestyén,你是对的。这是一个黑客。但是为了摆脱这个错误,我们需要对这些库(ui-select、valdr)或 hack 中的任何一个进行更改。
  • 确实如此。我们决定同意并接受此解决方案。我认为这两个项目的作者和贡献者都应该看看这个问题。感谢您的帮助。
猜你喜欢
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
相关资源
最近更新 更多