Set input invalid when typeahead-editable is false

后端 未结 2 1477
野趣味
野趣味 2021-01-04 04:17

I\'m using typeahead\'s UI Bootstrap component and I want to force the selection to validate my form.

Is it possible to configure it to set the input invalid when

相关标签:
2条回答
  • 2021-01-04 04:45

    The typeahead directive from http://angular-ui.github.io/bootstrap/ has already support for limiting inputs to matches (in other words, people can bind to model only values available as matches in the typeahead popup). You can do this by simply setting typeahead-editable='false' attribute.

    Please note that setting this attribute to false will not prevent people from typing-in invalid values. It will just make sure that a corresponding input is marked as invalid and a provided value is not bound to the model.

    0 讨论(0)
  • 2021-01-04 05:04

    Answer on the behalf of OP:

    var formValidatorsModule = angular.module('app.validator.formValidator', []);
    
    formValidatorsModule.directive('typeaheadForceSelection', function() {
        return {
            require : 'ngModel',
            link : function(scope, elm, attrs, ctrl) {
                ctrl.$parsers.push(function(viewValue) {
                    if (viewValue == undefined) {
                        ctrl.$setValidity('typeaheadForceSelection', false);
                    } else {
                        ctrl.$setValidity('typeaheadForceSelection', true);
                    }
                    return viewValue;
                });
            }
        };
    });
    
    0 讨论(0)
提交回复
热议问题