3

Here is a basic plunker that demonstrates the problem.

  • When you set the dropdownlist/select element model to empty you receive the required error message

  • But when you set the model from controller, and the model is not a option in the provided ng-options select element fails to show required. But the dropdown/select is in invalid state

Plunker

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

So how do we go about solving this problem?

1 Answer 1

1

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
};
Sign up to request clarification or add additional context in comments.

1 Comment

I had this solution as the last resort but seems i had taken right path without knowing it was the only path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.