angularjs select required not working ng-options

All we need is an easy explanation of the problem, so here it is.

I am trying to ensure a user selects a value from a select box before submitting the form.
This works correctly with static options; it however fails when populating the options via a model using ng-options

Examplehttp://plnkr.co/edit/wRqSCNskLo5c48jy4nQf

I am using AngularJS 1.2.9

Thank you in advance.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In your example changing the data bound select to the following fixes the required directive. I’m not sure why exactly.

<select ng-model="selectedValue1" ng-options="opt for opt in ['Mazda2','Maxda3']" required>
  <option></option>
</select>

Method 2

You can also try ng-required=”true” (it works on the latest AngularJS, not sure for 1.2.x).

<select ng-model="selectedValue1" ng-options="opt for opt in ['','Mazda2','Maxda3']" ng-required="true"></select>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply