I have an element that conditionally appears that is required to be filled out when visible. (using ng-required)
However, when it is not visible, I'm getting the following error:
An invalid form control with name='' is not focusable
How do I force ng-required to work ONLY if it element is visible. I do not want to enter novalidate in the form, because if I do, when the element is visible, the validation does not occur.
Like this, using a boolean for both ng-show
and ng-required
:
<form>
<input type="text" ng-show="displayCondition" ng-required="displayCondition"/>
</form>
Good question - a lot of people do not realize that passing false
into ng-required
disables the directive.
I solved it very easy with using ng-if instead of ng-show! Us only ng-if. When it is not visible the element is not in the code anymore. So the browser will not validate it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With