This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
select
with required
and only 1 option fails to validate (and set ng-model) until change
event is manually triggered #6288
Closed
Description
I have a select element, using ng-option over an array of integers
<form name="form">
<select ng-model="bar.foo" ng-options="foo as foo|filter for foo in fooarray" required>
<option ng-if="fooarray.length > 1" value="" selected>Choose</option>
</select>
<input type="submit" ng-disabled="form.$invalid" />
</form>
When the fooarray
has only 1 option, it should be chosen by default. Visually, it indeed is.
The DOM looks like this:
<select ng-model="bar.foo" ng-options="foo as foo|filter for foo in fooarray" class="ng-pristine form-control ng-invalid ng-invalid-required" required="">
<!-- ngIf: fooarray.length > 1 -->
<option value="0">filtered foo</option>
</select>
But:
- the select is considered invalid, thus the submit is always disabled
- the ng-model bar.foo is not set
- furthemore clicking on the select and re-choosing the same option doesn't trigger an onchange event (presumably because there was indeed no change)
So the form is never actually valid.
Triggering a change
event manually ($('select').trigger('change')
) indeed sets the model and makes the form valid.
It would seem it's related to the conditional <option>
, since when I remove it in the source, the select magically gets another (empty and default) option - but then the only option I wanted is no longer longer default, nor ..the only one. Which is IMHO a second bug.
Version: 1.2.13