I have a checkbox in angular that has a false value of undefined
and despite it not being required
, my form does not validate when it is checked and unchecked, due to ng-invalid
being added as a class to the input on uncheck.
Why does this happen?
Here is a plunker you can mess with: http://plnkr.co/edit/mFVcW1pI4wxf5lzRWoZE?p=preview
Notice the second checkbox has a false value of 'undefined'.
<input type="checkbox" ng-model="checkboxModel.value" ng-true-value="'hi'" ng-false-value="undefined" class="ng-pristine ng-untouched ng-valid">
Turns into
<input type="checkbox" ng-model="checkboxModel.value" ng-true-value="'hi'" ng-false-value="undefined" class="ng-dirty ng-invalid ng-invalid-parse ng-touched">
When unchecked, even though neither required
nor ng-required
are present
ng-model-options="{ allowInvalid: true }"
... but it doesn't actually work as I thought it did, so I deleted that answer. Anyway, the problem you're getting is aparse
error. Whenever a $parser returns undefined, that's aparse
error. So you can either use another value (likenull
) or figure out whyallowInvalid: true
doesn't work. - HankScorpio