0

I am having trouble on making validations from fields coming from a ng-repeat. Once I satisfy an error, every field with the same is as well satisfied. Is there any other way to do this? Like using the ng-model as validation indicator instead of name?

Here is my code:

HTML

<div ng-app="angularjs-starter" ng-controller="MainCtrl">
  <form name="myForm" novalidate >
   <fieldset  data-ng-repeat="choice in choices">
      <input type="text" ng-model="choice.name" name="fieldname" placeholder="Enter mobile number" required>
      <span ng-hide="!myForm.fieldname.$error.required">THIS FIELD IS REQUIRED {{ myForm.fieldname.$error.required }}</span>
      <button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
   </fieldset>
   <button class="addfields" ng-click="addNewChoice()">Add fields</button> 

   <div id="choicesDisplay">
      {{ choices }}
   </div>
</div>

JS

var app = angular.module('angularjs-starter', []);

    app.controller('MainCtrl', function($scope) {

    $scope.choices = [{id: 'choice1'}, {id: 'choice2'}];

    $scope.addNewChoice = function() {
      var newItemNo = $scope.choices.length+1;
      $scope.choices.push({'id':'choice'+newItemNo});
    };

    $scope.removeChoice = function() {
      var lastItem = $scope.choices.length-1;
      $scope.choices.splice(lastItem);
    };

});

PS.

I am aware that this can be done by manipulating the name attribute of the input with incremental but I was wondering if this can be done with less effort

2
  • I would use choice.id or choice.name as a name property. I don't see any other option. In order make validation work you need to have different names of the fields. Commented Mar 21, 2016 at 9:04
  • Ok thanks looks like, just testing my luck if there is any other choice Commented Mar 21, 2016 at 9:05

3 Answers 3

2
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
  <form name="myForm" novalidate >
   <fieldset  data-ng-repeat="choice in choices" data-ng-form="nestedMyForm" >
      <input type="text" ng-model="choice.name" name="fieldname" placeholder="Enter mobile number" required>
      <span ng-hide="!nestedMyForm.fieldname.$error.required">THIS FIELD IS REQUIRED {{ nestedMyForm.fieldname.$error.required }}</span>
      <button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
   </fieldset>
   <button class="addfields" ng-click="addNewChoice()">Add fields</button> 

   <div id="choicesDisplay">
      {{ choices }}
   </div>
</div>

Check how I added data-ng-form="nestedMyForm" hope that should work

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

1 Comment

@Dean Christiam Armada (thumbsup) :)
0

I don't think you can do it without adding a increment on the name field. You can do it easily with something like

<input type="text" ng-model="choice.name" name="fieldname_{{$index}}" required>

3 Comments

You were faster :) wanted to give the same answer
<span ng-hide="!myForm.fieldname_{{ $index }}.$error.required">THIS FIELD IS REQUIRED {{ myForm.fieldname.$error.required }}</span> will this be the code for this part as well?
I tried your suggestion but did not work. Making the name dynamic does not detect the validation properly. weird.
0

this should do the trick - notice how i changed the field name to be - field+$index so we can tell the difference betweeen each iteration inside the ng-repeat

<div ng-app="angularjs-starter" ng-controller="MainCtrl">   <form name="myForm" novalidate >    <fieldset  data-ng-repeat="choice in choices">
      <input type="text" ng-model="choice.name" ng-init="vm.name='field'+$index" name="{{vm.name}}" placeholder="Enter mobile number" required>
      <span ng-hide="!myForm.{{vm.name}}.$error.required">THIS FIELD IS REQUIRED {{ myForm.{{vm.name}}.$error.required }}</span>
      <button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>    </fieldset>    <button class="addfields" ng-click="addNewChoice()">Add fields</button> 

   <div id="choicesDisplay">
      {{ choices }}    </div> </div>

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.