1

I'm using angular-bootstrap-ui-dropdown on a form. I wanted to run some validation on this element. Is it possible with Angular Validator?

The code currently looks as follows, all of these are in a form element and I managed to validate other standard form elements (eg. input etc.)

<div class="btn-group" uib-keyboard-nav uib-dropdown>
    <button id="salutation-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
        <span class="dp-caption">{{passenger.salutation || 'Salutation'}}</span> <span
            class="fa fa-angle-down"></span>
    </button>
    <ul class="uib-dropdown-menu" role="menu" aria-labelledby="single-button">
        <li ng-repeat="salutation in salutationList" ng-click="setSalutation(salutation)"
            role="menuitem"><a href="#">{{salutation}}</a></li>
    </ul>
</div>

Is it possible to validate if there is a value selected in this dropdown?

2
  • you can use dropdowns in-build methods to validate . Commented Dec 4, 2015 at 5:29
  • @ngLover The documentation here doesn't seem to explain how I would go about doing that. Also, the applied classes on the dropdown doesn't seem to change based on the selection. Commented Dec 4, 2015 at 5:34

1 Answer 1

2

You could add $validators to the ngModelController (assuming you are using ngModel). For instance, in your directive you can require it and add the validators you could want:

variable with one simple list of allowed names:

var allowedNames = [...array with names...]

...and this could be part of the directive:

    ...
    require: '?ngModel',
    link: function (scope, iElement, iAttribute, ngModel) {
        var $validators = { // object with one or more validators
            isNameAllowed: function (modelValue, viewValue) {
                var value = modelValue || viewValue;

                if (!value || allowedNames.indexOf(value) > -1) {
                    return true;
                } else {
                    return false;
                }
            }
        };

        ngModel.$validators = angular.extend({}, ngModel.$validators, $validators);
        ...
    }
    ...

In this example, I have added a simple validator named "isNameAllowed" that checks if the value in the model is in the list of allowed names.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.