angularjs select required not working ng-options
Asked Answered
D

2

30

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.

Dobruja answered 23/7, 2014 at 21:45 Comment(1)
Isn't it because you didn't add any <option></option> ? See this plunkrSegalman
K
59

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>
Koestler answered 23/7, 2014 at 22:27 Comment(1)
It's the '<option></option>' tags that make it worksHallucinogen
A
3

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>
Akeylah answered 9/3, 2018 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.