Deeptechtons
Deeptechtons

Reputation: 11125

AngularJs doesn't validate a invalid dropdown selection

Here is a basic plunker that demonstrates the problem.

Plunker

http://plnkr.co/edit/gLtjRwkaaBOQG7YMvDav?p=preview

So how do we go about solving this problem?

Upvotes: 3

Views: 5480

Answers (1)

callmekatootie
callmekatootie

Reputation: 11228

Reference Documentation - Scroll to the bottom to the $error section.

$error only validates attributes of the select tag and not the option selected.

required is an attribute of the select tag - thus when the dropdown is empty, the $error flag is set to true.

However, assigning an option that is not among the predefined options is not handled by $error - you have to handle this yourself using something like:

<input type="button" value="Submit" ng-click="submitForm($event)">

And in your controller:

$scope.submitForm = function (event) {
    event.preventDefault();

    //Do your validation on the select value

    //If everything is fine, call the actual submit function
};

Upvotes: 1

Related Questions