Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularJS valdr + ui-select validation

I was googled on this issue but still can't find the right discussion about it. We are using valdr and ui select in a form ( angularJS app ) and we've faced the problem that the input that the ui-select renders won't get a name attribute, and since this, angular throws an error:

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">

So we were tried some hacks on the ui-select directive like templateCache rewrite / modify, hidden inputs with the same model but the result is the same.

Btw templateCache rewriting is the worst approach because of this directive used in appwide by other directives and we cannot hack on the whole app.

Did anyone faced with this problem?

Code snippet: http://plnkr.co/edit/sDNDDtnhi7Jxi9mtjDTl?p=preview

like image 571
Dániel Sebestyén Avatar asked Feb 11 '23 16:02

Dániel Sebestyén


1 Answers

If the name attribute is not present on input element valdr throws exception.

ui-select internally creates a input field without name attribute. Hence the error.

You can try this to get rid of the error.

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

like image 55
Vinay K Avatar answered Feb 13 '23 07:02

Vinay K