Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I need one help. Actually i am getting the following error while trying to validate the input field by taking the field name dynamically using Angular.js.

Error:

TypeError: Cannot read property '$invalid' of undefined

I am explaining my code below.

<form name="billdata" id="billdata"  enctype="multipart/form-data" novalidate>
   <div ng-repeat="mul in mulImage">
      <input type="file" class="filestyle form-control" data-size="lg" name="upload_{{$index}}" id="bannerimage_{{$index}}"  ng-model="mul.image" ngf-pattern="'image/*'" accept="image/*" ngf-max-size="2MB" ngf-select="onFileSelect1($index);">
   </div>
<input type="button" class="btn btn-success" ng-click="saveResturantDetails(billdata);"  id="saveData" value="Save"   style="margin-right:20px;"/>   </div>
</form>

Here i am uploading multiple images and when user will click on save button the it should first validate. I am explaining my controller side code below.

$scope.saveResturantDetails=function(billdata){
    if(billdata.$valid){
     }else{
      if(angular.isDefined($scope.mulImage)){
                for(var i=0;i<$scope.mulImage.length;i++){
                        var name='upload_'+i;
              if(billdata.name.$invalid){
                   alert('Please add the valid image(i.e-.png or .jpeg)  of size upto 2 mb max in image field'+(i+1));
                            return false;
                        }
                }
            }
     }
}

Actually i am getting the error in this if(billdata.name.$invalid) actually it can not take the proper name.If i am writing like this 'billdata.upload_0.$invalid' its working fine. But here i need to attach the name dynamically. Please help me.

share|improve this question
    
I like to add the object to my DOM so that I can visualize what is happening as I use a form. Perhaps try adding <pre>{{billdata | json }}</pre> to your view? – Daniel Shillcock Aug 25 at 7:04
up vote 1 down vote accepted

Would this work:

billdata[name].$invalid
share|improve this answer
    
Let me implement this. – subhra Aug 25 at 7:04
    
Yes,its working.Thank you. – subhra Aug 25 at 7:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.