bendrucker / ama

Ask me questions about building web applications
MIT License
6 stars 1 forks source link

Validating form that has required input in empty ng-repeat #15

Closed bsiddiqui closed 9 years ago

bsiddiqui commented 9 years ago
<form name="myForm">
  <div ng-repeat="friend in friends">
      <input type="radio" required ng-model="myFriend">    
  </div>
  <button ng-disabled="myForm.$invalid">Submit</submit>
</form>

If the the object that is being repeated is empty, angular doesn't display the inputs, allowing the form to invalid. I saw this discussion about using ng-form but it doesn't seem to work either if the ng-repeat isn't displayed.

http://stackoverflow.com/questions/12044277/how-to-validate-inputs-dynamically-created-using-ng-repeat-ng-show-angular

bendrucker commented 9 years ago

This is the downside of coupling validation to presentation via templates instead of data via code (:cry: Angular). The only way I know of for doing something like this would be to create a wrapper directive for the inputs that would take the ngModel and then create the inputs.