1

Hi I want to get the valid or invalid state of all the forms outside the form tag i.e suppose if any of the form is not valid error message should be shown. myform.$invalid is not working for all forms and is not updating

<div ng-repeat="a in [0,1,2,3,4]">
  <form name="myForm">
     <input type="text">
  </form>
</div>
<div ng-if="myform.$invalid">Fill all fields</div>

3 Answers 3

2

It is better to have all the <input> elements on the same form:

<form name="myForm">
  <div ng-repeat="a in [0,1,2,3,4]">
     <input type="text" name="myInput{{a}}" ng-model="itemArr[$index]" required />
  </div>
</form>
<div ng-show="myForm.$invalid">Fill all fields</div>
$scope.itemArr = [];

Also it is important that each <input> element has an ng-model directive.

For more information, see

Sign up to request clarification or add additional context in comments.

Comments

1

As per my understanding angular validation is not working properly with same form name in same page.

Angular will only consider the last form name

Ex:

<form name="myForm">
  <input name="myInput" ng-model="myInput" required>
</form>
<form name="myForm">
  <input type="email" name="myInpu" ng-model="myInpu">
</form>
<p>The input's valid state is:</p>
<h1>Form 1 : {{myForm.myInput.$valid}}</h1>
<h1>Form 2 : {{myForm.myInpu.$valid}}</h1>

Comments

0

I am having the same concern here, where I am creating multiple instance of form with same format (it's a table for multiple row input). I have been trying adding the $index to the form name but then I am facing issue to access the form_$index inside ng-messages and ng-click

example:

<div ng-repeat="a in [0,1,2,3,4]">
  <form name="myForm_{{ ::$index }}">
     <input type="text">
  </form>
</div>
<div ng-if="myform_{{ ::$index }}.$invalid">Fill all fields</div>

Wondering if anyone else can propose a solution for this use case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.