7

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.

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • 3
    If you're using ng-hide, have you considered using ng-if instead (which removes the element from the dom completely)? – Joao Mar 12 '15 at 22:06

2 Answers2

9

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.

Andrew Templeton
  • 1,656
  • 10
  • 13
2

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.

elpeyotl
  • 372
  • 1
  • 3
  • 14